Christian Klemm
Christian Klemm

Reputation: 1505

Regex, not statement

Heyho,

I have the regex

([ ;(\{\}),\[\'\"]?)(_[a-zA-Z_\-0-9]*)([ =;\/*\-+\]\"\'\}\{,]?)

to match every occurrence of

_var

Problem is that it also matches strings like

test_var

I tried to add a new matching group negating any word character but it didn't worked properly.

Can someone figure out what I have to do to not match strings like var_var?

Thanks for help!

Upvotes: 1

Views: 122

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626747

You can use the following "fix":

([[ ;(){},'"]?)(\b_[a-zA-Z_0-9-]*\b)([] =;/*+"'{},-]?)
                ^                 ^

See regex demo

The word boundary \b is an anchor that asserts the position between a word and a non-word boundary. That means your _var will never match if preceded with a letter, a digit, or a . Also, I removed overescaping inside the character classes in the optional capturing groups. Note the so-called "smart placement" of hyphens and square brackets that for a Python regex might be not that important, but is still a best practice in writing regexes. Also, in Python regex you don't need to escape / since there are no regex delimiters there.

And one more hint: without u modifier, \w matches [a-zA-Z0-9_], so you can write the regex as

([[ ;(){},'"]?)(\b_[\w-]*\b)([] =;/*+"'{},-]?)

See regex demo 2.

And an IDEONE demo (note the use of r'...'):

import re
p = re.compile(r'([[ ;(){},\'"]?)(\b_[\w-]*\b)([] =;/*+"\'{},-]?)')
test_str = "Some text _var and  test_var"
print (re.findall(p, test_str))

Upvotes: 1

Related Questions