3

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

4
  • I, too, get results on both strings. What is the output of python --version? Commented Sep 19, 2015 at 1:10
  • Ok, the screenshot I uploaded shows a typo in my pattern, I had a "+" after the [^\.] should I delete this question? Commented Sep 19, 2015 at 1:15
  • What if you use: pattern = r"^.*W([0-9]+(\.5)?)[^\.]?.*$" ? Do you still get None? Commented Sep 19, 2015 at 1:17
  • Great! Going to update the answer, you're missing the ^ at the beginning of pattern definition. Commented Sep 19, 2015 at 1:20

3 Answers 3

1

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)

Sign up to request clarification or add additional context in comments.

Comments

1

When I run the code you provided, I get return values on both:

$ python3 test.py
<_sre.SRE_Match object at 0x6ffffedc3e8>
<_sre.SRE_Match object at 0x6ffffedc3e8>

2 Comments

but it is followed by a "*" meaning that the "." must be matched "zero or more times". According to docs.python.org/3/library/re.html
@wesanyer: Sorry, you're right. But now that I've tested it, it seems like your code works fine.
1

I've tried the exact same code and I have a match for both cases:

python3.4:

Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 23 2015, 02:52:03) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import re
>>> pattern = r".*W([0-9]+(\.5)?)[^\.]?.*$"
>>> string1 = '6013-SFR6W4.5'
>>> print(re.match(pattern, string1))
<_sre.SRE_Match object; span=(0, 13), match='6013-SFR6W4.5'>
>>> string2 = '6013-SFR6W4.5L'
>>> print(re.match(pattern, string2))
<_sre.SRE_Match object; span=(0, 14), match='6013-SFR6W4.5L'>

python 2.7:

Python 2.7.6 (default, Sep  9 2014, 15:04:36) 
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.39)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import re
>>> pattern = r".*W([0-9]+(\.5)?)[^\.]?.*$"
>>> string1 = '6013-SFR6W4.5'
>>> print(re.match(pattern, string1))
<_sre.SRE_Match object at 0x10abf83e8>
>>> string2 = '6013-SFR6W4.5L'
>>> print(re.match(pattern, string2))
<_sre.SRE_Match object at 0x10abf83e8>

Try use pattern = r"^.*W([0-9]+(\.5)?)[^\.]?.*$", with ^at the beginning.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.