Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python 3 Regex Issue

Why does the following string1 regexp not match? I have tested it using this and it appears my regex-fu is accurate, so I figure I must be missing something in the python implementation:

import re
pattern = r".*W([0-9]+(\.5)?)[^\.]?.*$"
string1 = '6013-SFR6W4.5'
string2 = '6013-SFR6W4.5L'
print(re.match(pattern, string1)) # the return value is None
print(re.match(pattern, string2)) # this returns a re.match object

Here is a screenshot of an interactive session showing this issue.

EDIT

sys.version outputs 3.4.3

like image 522
wesanyer Avatar asked Feb 10 '26 18:02

wesanyer


1 Answers

In the code you posted, you have:

pattern = r".*W([0-9]+(\.5)?)[^\.]?.*$"

But in the code from your screenshot, you have

pattern = r".*W([0-9]+(\.5)?)[^\.]+.*$"

(Note the ? near the end of the first pattern is replaced with a + in the second one)

like image 70
Myk Willis Avatar answered Feb 13 '26 10:02

Myk Willis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!