DineshKumar
DineshKumar

Reputation: 1739

Python regex dealing with "?"

I know it's a pretty simple question. I happened to look at a regex example.

import re
pattern = r'^M?M?M?$'

s = "MDM"

re.search(pattern, s)

May I know why it doesn't match the string s? AFAIK, ? is to specify 0 or 1 occurence. It matches MMM though.

However the same string matches when the pattern is r'M?M?M?$' or r'^M?M?M?'. I am not getting what makes the difference here. Could someone please explain?

Upvotes: 2

Views: 106

Answers (3)

awesoon
awesoon

Reputation: 33651

r'^M?M?M?$' is the same as r'^M{0,3}$'. So, your pattern accepts '', 'M', 'MM', 'MMM' strings.

r'M?M?M?$' is the same as r'M{0,3}$ and, actually, accepts all strings, since there is always an empty part at the end of the string:

In [21]: pattern = r'M?M?M?$'

In [22]: re.search(pattern, 'A string without capital m at all')
Out[22]: <_sre.SRE_Match object; span=(33, 33), match=''>

Upvotes: 2

Maroun
Maroun

Reputation: 95948

The regex

M?M?M?$

matches the last "M" in "MDM". But when you add ^ (beginning of string), it'll try to match from the beginning and it'll fail because M? matches 0 or 1 "M", but not a "D".

On the other regex:

^M?M?M?

The first "M" is matched.

Upvotes: 1

Simulant
Simulant

Reputation: 20102

^ matches the start of a line. $ matches the end of the line.

so 'M?M?M?$' matches the last M in MDM and '^M?M?M?' matches the first M in MDM.

'^M?M?M?$' cannot match MDM because of the D in the middle that is not listed in your regex and the requirement to match the start of the line and the end of the line while there are 0, 1, 2 or 3 M in between.

Upvotes: 1

Related Questions