re. module

Amit Patel amitp at Xenon.Stanford.EDU
Fri Nov 23 13:01:09 EST 2001


 Adrian Leu <AdrianLeu at kelseus.com> wrote:
| I am trying to figure out a solution for the following:
| 
| import re
| 
| text = 'name_0 goto place'
| string = 'name|name_0|name_1'
| 
| pattern = re.compile(string, re.I)
| d = pattern.search(text)
| return d.group(0)
| 
| The problem is that my program will return 'name' to this. What I want
| is for the search to return a match (if it exists) only after looking
| at the whole string (name_0 in this case). I.e. in my example I would
| like the program to return 'name_0' not 'name'.

Rearrange the pattern to 'name_0|name_1|name'.  :-( 

Most of the time, regular expressions will find the 'longest match',
but in this case it doesn't work.  It's a real pain when writing a
parser generator.  I'd really like to just | together the tokens to
match, but because of the above.. er.. "feature", it doesn't work so
well.  (And because the parser generator takes a generic set of
tokens, it doesn't know which one is a subset of another.)

Another option in your example would be if you're willing to match an
entire word, there's some re pattern (\b??) that matches the end of a
word.

	- Amit


-- 
--
Amit J Patel, Computer Science Department, Stanford University
http://www-cs-students.stanford.edu/~amitp/



More information about the Python-list mailing list