[Tutor] regex: not start with FOO

Bernard Rankin berankin99 at yahoo.com
Tue Feb 3 17:18:40 CET 2009




> > I'd like to match any line that does not start with FOO.  (Using just a reg-ex 
> rule)
> >
> > 1) What is the effective difference between:
> >
> > (?!^FOO).*
> >
> > ^(?!FOO).*
> 
> One difference is that the first will match starting anywhere in a
> string, while the second will match only at the start. For this exact
> example I don't think it matters but if you replace .* with something
> else you can see a difference. For example:
> In [52]: re.findall('(?!^FOO) in', 'in in in')
> Out[52]: [' in', ' in']
> 
> In [53]: re.findall('^(?!FOO) in', 'in in in')
> Out[53]: []
> 
> I think I would use the second form, it seems to more directly express
> what you mean.
> 

Hmm...

In [30]: re.findall('(?!FOO)in', 'in FOOin in')
Out[30]: ['in', 'in', 'in']


OK, now I am confused... . why am I getting 3 results?



      



More information about the Tutor mailing list