[Tutor] regex: not start with FOO

Bernard Rankin berankin99 at yahoo.com
Tue Feb 3 17:12:59 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.
> 

Thank you... that clarifies things greatly.

Now, to change the example slightly:

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

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

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

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

In [7]: re.findall('(?!^FOO$)in', 'in in in')
Out[7]: ['in', 'in', 'in']


What is the effective difference between numbers 4 thru 7?

That is, what effect does a string position anchor have within the sub expression?

Thank you,
:)


      



More information about the Tutor mailing list