Trying to match any number of comma separated 7 character strings that can include digits, _ and ?.
x = re.compile(r"^([0-9_\?]{7})(,\1)*$")
>>> x.match("123456?")
<_sre.SRE_Match object at 0x0046C800>
>>> x.match("12345??")
<_sre.SRE_Match object at 0x023483C8>
>>> x.match("1234???")
<_sre.SRE_Match object at 0x0046C800>
>>> x.match("123????")
<_sre.SRE_Match object at 0x023483C8>
>>> x.match("12?????")
<_sre.SRE_Match object at 0x0046C800>
>>> x.match("1??????")
<_sre.SRE_Match object at 0x023483C8>
>>> x.match("???????")
<_sre.SRE_Match object at 0x0046C800>
>>> x.match("???????,1234567")
>>>
^^^^^^^^^^^^^^^^^^^^^^This is where it fails
vvvvvvvvvvvvvvvvvvvvvvBut repetition works if I don't have a ? in the string
>>> x.match("1234567,1234567")
<_sre.SRE_Match object at 0x023483C8>
I've also tried it with:
x = re.compile(r"^([0-9_\\?]{7})(,\1)*$")
But that just allows it to match the \ character (as expected).
What is wrong with my regex?
\1 is a backreference that will match what the referenced group matched, not what it can match. If you want to allow that pattern to appear twice, just write it twice:
r"^([0-9_?]{7})(,[0-9_?]{7})*$"
(Also note that ? doesn’t need escaping inside a character set.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With