Pattern Matching

Christopher T King squirrel at WPI.EDU
Mon Jul 19 15:10:50 EDT 2004


On Mon, 19 Jul 2004, Greg Lindstrom wrote:

> The following does not work, but is the flavor of what I want to do:
> 
> long_line_of_text = 'Start: 1/1/2004 and some stuff.~Start: 2/3/2004 stuff.
> ~Start 5/1/2004 morestuff.~'
> while re.match('Start:\ (\D?/\D?/\D+)', long_line_of_text):
>     # process the date string here which I hoped to catch in the parenthesis
>       above.
> 
> I'd like this to keep matching and processing the string as long as it keeps
> matching the pattern, bopping down the string as it goes.

That line tastes distincly Perlish ;)

What you want to write in Python is:

for match in re.finditer('Start:\ (\D?/\D?/\D+)', long_line_of_text):
   <do something with match.group(1)>

re.finditer() returns an iterator that loops over all occurances of the 
pattern in the string, returning a match object for each one.  
match.group() returns the actual text of the match, and match.group(n) 
returns the text of group n.

I'm curious, though, why do you escape the space?  My guess is it's 
something from Perl that I don't remember.




More information about the Python-list mailing list