-4

I need a regular expression for python 2.7 that checks if a given word consists only of the letters 'a', 'b', 'r', 'e', 'd'. Please Help!

I tried the following code:

text = raw_input ('Enter Text:')

result = re.match(r'[abred]+', text)

if result:
    print ('Match')      
else :
    print ("Doesn't Match")

However, for example by inputting "aaaq" it counts it as a matching text.

Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321
  • Google it google it gooooooooogle it. – Andy Guibert Apr 30 '15 at 18:33
  • Whenever I put down this code, if it sees one of the selected letters it counts it as true, but what I need is the words which has only those letters –  Apr 30 '15 at 18:34
  • Listen, I don't need you to do anything I just thought this forum is friendly to beginners and I was asking a question that I am personally interested in. If you don't like anything related to this you are free to leave. –  Apr 30 '15 at 18:37
  • I did I am not 100% sure if I did it correctly though... This is the code that I tried: –  Apr 30 '15 at 18:41
  • text = raw_input ('Enter Text:') result = re.match(r'[abred]+', text) if result: print ('Match') else : print ("Doesn't Match") –  Apr 30 '15 at 18:41
  • For example whenever I input aaaaq it counts it as a matching text –  Apr 30 '15 at 18:42

2 Answers2

2

You don't need a regular expression you can use str.translate:

In [4]: s = "bearded"  
In [5]: not s.translate(None,"bread")
Out[5]: True
In [6]: s = "bored"
In [7]: not s.translate(None,"bread")
Out[8]: False

Your regex re.match(r'[abred]+', text) won't work because it matches any of the letters that appear in the string, it does not mean that you only have those letters.

You could re.sub the letters and see if the string is empty:

if not re.sub(r'[abred]',"", text)

Or use search ignoring those letters so if there is any character bar those you will get a match:

if not re.search("[^bread]",text):
Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321
  • But the problem is I do need a regular expression. My assignment was to do so, but for some reason my code does things wrong every single time... –  Apr 30 '15 at 18:38
  • 1
    @MelsKolozyan,add the code you tried to your question. – Padraic Cunningham Apr 30 '15 at 18:38
  • This is the code I tried: text = raw_input ('Enter Text:') result = re.match(r'[abred]+', text) if result: print ('Match') else : print ("Doesn't Match") –  Apr 30 '15 at 18:40
  • @MelsKolozyan, better to add the code to your question, that would not work because it will match `"bread"` in `"breads"` but that does not mean only those letters appear, if you tried to re.sub those letters and see if the string is empty that would work, `not re.sub(r'[abred]+',"", "breads")` – Padraic Cunningham Apr 30 '15 at 18:43
  • If it isn't hard for you, can you write the regexp and then I'll try to figure out how it works. Of course if it isn't too hard for ya. Thank you! –  Apr 30 '15 at 18:49
  • @PadraicCunningham You can match the end of a string with $. – Shashank Apr 30 '15 at 18:53
  • @Shashank, I don't follow, I presumed there was only those five letters allowed so it would have to be a single word. – Padraic Cunningham Apr 30 '15 at 18:55
  • @PadraicCunningham You can do it with `re.match` which is what he's hinting to, I believe – neatnick Apr 30 '15 at 18:57
  • @SwankSwashbucklers, you can also use search and ignore those letters, if there are any other characters you will get a match – Padraic Cunningham Apr 30 '15 at 19:11
1

In order to modify your regex to work you need to use the ^ and $ characters. ^ matches the beginning of the string, and $ matches the end of the string. So if you were to modify your regex to be ^[abred]+$ it would match strings that only contained the letters a, b, r, e, and d. As opposed to the current regex ([abred]+), which will match any string that had those letters in it.

neatnick
  • 1,489
  • 1
  • 18
  • 29
  • The regular uncompiled `re.match` only searches for patterns in the string that are prefixes of the string. So, with that particular function, '^' is optional, although it certainly doesn't hurt to have it. The '$' is definitely necessary though. – Shashank Apr 30 '15 at 19:47