iGwok
iGwok

Reputation: 333

trouble understanding regular expressions, python

I am quite new to the re module in python, but have been trying to write a regular expression to grab the version number of a file. In most cases this snippet seems to work:

test = "filename.ver3_576.exr"
print(re.search("(?!(v|ver|version|vers))\d+", test.lower()).group())

but if I change the test string a little, it does not give me the results I would expect:

test2 = "filename.ver_3_576.exr" # expects None, because of the underscore, gets 3
test3 = "filenameVe2_version201_1001.exr" # expects 201, gets2, "ve"(exactly) is not something I want to search for

I am obviously doing something wrong here, but a struggling to identify what that might be.

Any help would be greatly appreciated, cheers

Upvotes: 2

Views: 66

Answers (1)

Ionut Hulub
Ionut Hulub

Reputation: 6326

re.search('(version|vers|ver|v)(\d+)', test.lower()).group(2)

To answer your comment, you didn't use a lookbehind expression. That's a negative lookahead expression. The expression you used is identical to '\d+' (not so easy to explain why).

It's not easy to use a positive lookbehind re in this case because it requires a fixed width pattern. The following re, for example, will throw an error: '(?<=(version|vers|ver|v))\d+', so I suggest you use the re that I posted bucause it's the most streight forward.

Upvotes: 2

Related Questions