I am trying to use the regex part as input for the later part of the regex.
What I still have (which I could not say):
import re regex = re.compile(r"(?P<length>\d+)(\d){(?P=length)}") assert bool(regex.match("3123")) is True assert bool(regex.match("100123456789")) is True
Destroying this, the first digit denotes the number of digits that must be matched subsequently. In the first statement, I get a 3
as the first character, which means that there must be exactly three digits after it, otherwise more than 9 digits. If there are more than 9 digits, then the first group should be expanded and checked for the rest of the digits.
The regular expression 3(\d){3}
will correctly match the first statement, however I cannot get the regular expression according to the general case when the brackets {}
get the regular expression backreference: {(?P=length)}
Call a regex with the re.DEBUG
flag:
subpattern 1 max_repeat 1 4294967295 in category category_digit subpattern 2 in category category_digit literal 123 groupref 1 literal 125
Curly braces {
( 123
) and }
( 125
) appear to be interpreted as literals when there is a backreference inside them.
When there is no backlink, for example {3}
, I see that {3}
interpreted as max_repeat 3 3
Is using a backreference as part of a regular expression possible?
source share