Regular expression issue

MRAB python at mrabarnett.plus.com
Sun Aug 8 18:50:52 EDT 2010


genxtech wrote:
> I am trying to learn regular expressions in python3 and have an issue
> with one of the examples I'm working with.
> The code is:
> 
> #! /usr/bin/env python3
> 
> import re
> 
> search_string = "[^aeiou]y$"

You can think of this as: a non-vowel followed by a 'y', then the end of 
the string.

> print()
> 
> in_string = 'vacancy'
> if re.search(search_string, in_string) != None:
> 	print(" ay, ey, iy, oy and uy are not at the end of {0}.".format(in_string))
> else:
> 	print(" ay, ey, iy, oy or uy were found at the end of {0}.".format(in_string))

Matches because 'c' is a non-vowel, 'y' matches, and then the end of the 
string.

> print()
> 
> in_string = 'boy'
> if re.search(search_string, in_string) != None:
> 	print(" ay, ey, iy, oy and uy are not at the end of {0}.".format(in_string))
> else:
> 	print(" ay, ey, iy, oy or uy were found at the end of {0}.".format(in_string))

Doesn't match because 'o' is a vowel, not a non-vowel.

> print()
> 
> in_string = 'day'
> if re.search(search_string, in_string) != None:
> 	print(" ay, ey, iy, oy and uy are not at the end of {0}.".format(in_string))
> else:
> 	print(" ay, ey, iy, oy or uy were found at the end of {0}.".format(in_string))

Doesn't match because 'a' is a vowel, not a non-vowel.

> print()
> 
> in_string = 'pita'
> if re.search(search_string, in_string) != None:
> 	print(" ay, ey, iy, oy and uy are not at the end of {0}.".format(in_string))
> else:
> 	print(" ay, ey, iy, oy or uy were found at the end of {0}.".format(in_string))

Doesn't match because 't' is a non-vowel but 'a' doesn't match 'y'.

> print()
> 
> The output that I am getting is:
>    ay, ey, iy, oy and uy are not at the end of vacancy.
>    ay, ey, iy, oy or uy were found at the end of boy.
>    ay, ey, iy, oy or uy were found at the end of day.
>    ay, ey, iy, oy or uy were found at the end of pita.
> 
> The last line of the output is the opposite of what I expected to see,
> and I'm having trouble figuring out what the issue is.  Any help would
> be greatly appreciated.




More information about the Python-list mailing list