[Tutor] regex: not start with FOO

Kent Johnson kent37 at tds.net
Tue Feb 3 12:40:59 CET 2009

On Mon, Feb 2, 2009 at 5:46 PM, Bernard Rankin <berankin99 at yahoo.com> wrote:
> Hello,
> 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.


More information about the Tutor mailing list