This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: regex mismatch in the simple cases
Type: behavior Stage: resolved
Components: Regular Expressions Versions: Python 3.5
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: MaxR, ezio.melotti, mrabarnett, steven.daprano
Priority: normal Keywords:

Created on 2017-01-30 02:05 by MaxR, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (2)
msg286464 - (view) Author: (MaxR) Date: 2017-01-30 02:05
When I compile and match, for example:
pattern = re.compile(r"""    ([a-z]|\d)+$         # ends with letter or number only    """, re.VERBOSE | re.IGNORECASE)
print(re.match(pattern, 'abc'))

result is correct:
<_sre.SRE_Match object; span=(0, 3), match='abc'>

but then I use the same pattern with another string:
print(re.match(pattern, 'abc.fds'))

result is: 
None

I tried to reformulate the pattern to the same, for example:
pattern = re.compile(r"""    (a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z|\d)+$         # ends with letter or number only    """, re.VERBOSE | re.IGNORECASE)

or to:
pattern = re.compile(r"""    [a-z\d]+$         # ends with letter or number only    """, re.VERBOSE | re.IGNORECASE)

or to:

pattern = = re.compile(r"""    ([a-z]|\d)+$         # ends with letter or number only    """, re.VERBOSE | re.IGNORECASE)

but the result is all the time the same - None
And еhat is the double strange for the last three pattern - None is result also for 
print(re.match(pattern, 'abc'))

I checked this patterns on the site regex101.com
and all of them should work good and right
It's achtung!
msg286467 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2017-01-30 02:23
re.match only matches at the start of the string. If you use re.search instead, you will get the results you are expecting.
History
Date User Action Args
2022-04-11 14:58:42adminsetgithub: 73574
2017-01-30 02:23:37steven.dapranosetstatus: open -> closed

nosy: + steven.daprano
messages: + msg286467

resolution: not a bug
stage: resolved
2017-01-30 02:05:32MaxRcreate