re module - cannot make expression

L

Laszlo Zsolt Nagy

Hi All!

I would like to match strings not beginning with '/webalizer'. How can I
do this?
The only one negation command is ^ inside [] but it cannot be used here.
I looked
over "re" examples on the net but I could not find an example about how
to match
"not beginning with" type expressions.

Thanks,

Laci 2.0
 
S

Steve Holden

Laszlo said:
Hi All!

I would like to match strings not beginning with '/webalizer'. How can I
do this?
The only one negation command is ^ inside [] but it cannot be used here.
I looked
over "re" examples on the net but I could not find an example about how
to match
"not beginning with" type expressions.

Thanks,

Laci 2.0
You can do taht with Python re's. Look in the re documentation for
"negative lookahead assertion".

regards
Steve
 
S

Steven Bethard

Laszlo said:
I would like to match strings not beginning with '/webalizer'. How can I
do this?

Are you sure you need a regular expression? The str.startswith method
is what I would normally use to solve this kind of problem:

py> lst = ['aax', 'abx', 'acx', 'aay', 'aby', 'acy']
py> [s for s in lst if not s.startswith('ab')]
['aax', 'acx', 'aay', 'acy']

If you do need a regular expression, Steve Holden's suggestion about
negative lookahead assertions is the right way to go:

py> s = 'aaxabxacxaayabyacy'
py> re.compile(r'(?!ab).{2}[xy]+').findall(s)
['aax', 'acx', 'aay', 'acy']

Steve
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,219
Messages
2,571,117
Members
47,727
Latest member
PasqualePf

Latest Threads

Top