1

I am trying to write a regular expression in Python which takes a string and checks if:

  1. The last character is a vowel.
  2. The last 2 characters are not the same.

This is what I came up with:

[aeiou]$

Can anybody help me with point number 2: last 2 characters are not the same. For example, expresso is valid and expressoo is not valid.

3
  • Why not "expressoo"? (By the way, it's "espresso".) Commented Dec 1, 2010 at 1:15
  • Huh? The first and last character of "expressoo" are not the same, so that should be valid. Did you mean "oxpresso"? Commented Dec 1, 2010 at 1:15
  • @Steve, Sorry i edit it. Have a look again Commented Dec 1, 2010 at 1:17

3 Answers 3

5

It might be easier to do this without a regular expression.

e.g if s[-2]!=s[-1] and s[-1] in 'aeiou'

Sign up to request clarification or add additional context in comments.

6 Comments

The question changed. Nevertheless, +1.
you would want to check for length first, in order to be complete, right? Something like: if len(s)>=2 and s[-1]!=s[-2] and s[-1] not in "AEIOUaeiou"
I have to do it using regular expressions. I have done with that way already. Thank anyway...
Shouldn't it be s[-2] instead of s[0]?
The question changed after I answered it. Previously it was required that the first and last characters not be the same.
|
2
(?i)([aeiouy])(?!\1)[aeiouy]$

EDIT:

This is also appealing for not having a repeat:

(?i)(?=[aeiouy]{2}$)(.)(?!\1).

5 Comments

(note that you need to escape the backslash when you put it in your sourcecode).>>> re.match("(?i)([aeiouy])(?!\\1)[aeiouy]$", "ao") <_sre.SRE_Match object at 0x42eca0> >>> re.match("(?i)([aeiouy])(?!\\1)[aeiouy]$", "oo") >>>
$ perl -le 'print for q(import re), q(print re.compile(r"(?i)([aeiouy])(?!\1)[aeiouy]$").search("paperboy").group(0))' | python produces the string oy.
@I82Much: You’re trying too hard. :)
@Ricky2002: Don’t use web sites!!! Try it in your shell where you know WTH’s going on!
The other solution reminds me of (?i)(?=.*a)(?=.*e)(?=.*i)(?=.*o)(?=.*u)(?=.*y).
0

Best I can do:

r"\w*(?:[^a\W]a|[^e\W]e|[^i\W]i|[^u\W]u|[^o\W]o)\b"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.