re. module

Adrian Leu AdrianLeu at kelseus.com
Thu Dec 6 05:37:10 EST 2001


amitp at Xenon.Stanford.EDU (Amit Patel) wrote in message news:<9tm2t5$o66$1 at usenet.Stanford.EDU>...
> 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
> 
> 
> --

Thanks ! Yes, the r'\b' pattern did the trick.



More information about the Python-list mailing list