python regex has unexpected behaviour

Dennie
Dennie used Ask the Experts™
on
Hi,

I’m trying to match a string ‘test’, it can only be matched if it’s not directly preceded by ‘no’ with any number of spaces after ‘no’.

So this underlined should match:
test
Bla bla test blab la
Bla no bla test
Bla no test
bla bla no       test

I've got
m = re.search('[^no *]test', line)

Open in new window


The problem is my code does not match 'test' if no characters are preceded by 'test'.
so '<space>test' does match and 'test' will not. What am I missing?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Your expression it totally wrong.

You are saying that there should be one character before test which cannot be n, o, space or *.

Author

Commented:
Oke strange, regex buddy shows that's it's correct... Can you help me with the correct regex?
kaufmedGlanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015

Commented:
Oke strange, regex buddy shows that's it's correct...
I promise you that you are interpreting Regex Buddy's results incorrectly. It is as farzanj as mentioned.

Try this instead:

m = re.search('^(?!.*no *test).*?test', line)

if m:
    print(m.group(0))

Open in new window

Become a CompTIA Certified Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

Author

Commented:
@kaufmed

sre_constants.error: look-behind requires fixed-width pattern

Any idea how to work around this problem with python?

Author

Commented:
Oops my bad, copied the wrong regex. It seems to be working.

However the regex doesn't seem to match:
bla no test blabla test

Any ideas how to fix this?
ozo
Most Valuable Expert 2014
Top Expert 2015

Commented:
(?!no *test)(^.?|.\S) *test

Author

Commented:
@ozo your solution doesn't seem to work
Glanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015
Commented:
Can you provide updated inputs? Her solution appeared to work for me.

import re

inputs = {}
inputs[0] = "test"
inputs[1] = "Bla bla test blab la"
inputs[2] = "Bla no bla test"
inputs[3] = "Bla no test"
inputs[4] = "bla bla no       test"
inputs[5] = "bla no test blabla test"

for i in inputs:
	m = re.search('(?!no *test)(^.?|.\S) *test', inputs[i])
	if m:
		print(m.group(0))
	else:
		print('[no match]')

Open in new window

kaufmedGlanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015

Commented:
Most (if not all) of the credit should really go to ozo--it was here pattern; I just demoed it.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial