[Tutor] String matching

Max Noel maxnoel_fr at yahoo.fr
Fri Nov 26 17:59:38 CET 2004


On Nov 26, 2004, at 16:07, Bernard Lebel wrote:

> >>> import re
> >>> mystring = 'helloworldmynameisbernardlebel'
>
> Now I wish to attempt a match with 'bernard'.
>
> >>> if not re.compile( 'bernard' ).match( mystring ) == None:
> ... 	print 'success'
>
> Well, nothing gets printed. Could anyone tell me how to achieve that 
> simple type of matching?

	You need to use search(), not match(). match() tries to match the 
pattern to the beginning of the string. IOW, in your case it tries to 
see if mystring begins with "bernard".

-- Max
maxnoel_fr at yahoo dot fr -- ICQ #85274019
"Look at you hacker... A pathetic creature of meat and bone, panting 
and sweating as you run through my corridors... How can you challenge a 
perfect, immortal machine?"



More information about the Tutor mailing list