Try it here
Subscribe
Python regular expression

Named Groups and Backreferences

named_groups_and_backreferences

FeatureSyntaxDescriptionExamplePython
Named capturing group (?<name>regex) Captures the text matched by “regex” into the group “name”. The name can contain letters and numbers but must start with a letter. (?<x>abc){3} matches abcabcabc. The group x matches abc. nononononononononononono
Named capturing group (?'name'regex) Captures the text matched by “regex” into the group “name”. The name can contain letters and numbers but must start with a letter. (?'x'abc){3} matches abcabcabc. The group x matches abc. nononononononononononononono
Named capturing group (?P<name>regex) Captures the text matched by “regex” into the group “name”. The name can contain letters and numbers but must start with a letter. (?P<x>abc){3} matches abcabcabc. The group x matches abc. nonononoYESnonononononononononono
Duplicate named group Any named group Two named groups can share the same name. (?<x>a)|(?<x>b) matches a or b. n/an/aerrorn/an/an/an/an/an/an/an/an/a
Duplicate named group Any named group Named groups that share the same name are treated as one an the same group, so there are no pitfalls when using backreferences to that name. n/anonononononon/an/an/an/anon/anon/an/an/an/an/an/an/an/a
Duplicate named group Any named group If a regex has multiple groups with the same name, backreferences using that name point to the leftmost group in the regex with that name. nonon/anonon/an/an/an/anon/anon/an/an/an/an/an/an/an/a
Duplicate named group Any named group If a regex has multiple groups with the same name, backreferences using that name point to the leftmost group with that name that has actually participated in the match attempt when the backreference is evaluated. nonon/an/an/an/an/anon/an/an/an/an/an/an/an/an/a
Duplicate named group Any named group If a regex has multiple groups with the same name, backreferences using that name point to the rightmost group with that name that appears to the left of the backreference in the regex. nonon/anonononononon/an/an/an/anon/an/an/an/an/an/an/an/an/a
Duplicate named group Any named group If a regex has multiple groups with the same name, backreferences using that name can match the text captured by any group with that name that appears to the left of the backreference in the regex. nonon/anonononononon/an/an/an/an/anon/an/an/an/an/an/an/an/a
Named backreference \k<name> Substituted with the text matched by the named group “name”. (?<x>abc|def)=\k<x> matches abc=abc or def=def, but not abc=def or def=abc. nononononononononononono
Named backreference \k'name' Substituted with the text matched by the named group “name”. (?'x'abc|def)=\k'x' matches abc=abc or def=def, but not abc=def or def=abc. nononononononononononononono
Named backreference \k{name} Substituted with the text matched by the named group “name”. (?'x'abc|def)=\k{x} matches abc=abc or def=def, but not abc=def or def=abc. nonononononononononononononononono
Named backreference \g{name} Substituted with the text matched by the named group “name”. (?'x'abc|def)=\g{x} matches abc=abc or def=def, but not abc=def or def=abc. nonononononononononononononononono
Named backreference (?P=name) Substituted with the text matched by the named group “name”. (?P<x>abc|def)=(?P=x) matches abc=abc or def=def, but not abc=def or def=abc. nononononoYESnonononononononononono
Failed backreference Any named backreference Backreferences to groups that did not participate in the match attempt fail to match. (?<x>a)?\k<x> matches aa but fails to match b. n/an/aYESn/an/an/an/an/an/an/an/an/a
Nested backreference Any named backreference Backreferences can be used inside the group they reference. (?<x>a\k<x>?){3} matches aaaaaa. n/an/a2.4–3.4 failn/an/an/an/an/an/an/an/an/a
Forward reference Any named backreference Backreferences can be used before the group they reference. (\k<x>?(?<x>a)){3} matches aaaaaa. n/an/aerrorn/an/an/an/an/an/an/an/an/a
Named capturing group Any named capturing group A number is a valid name for a capturing group. (?<17>abc){3} matches abcabcabc. The group named “17” matches abc. n/an/aerrorn/an/an/an/an/an/an/an/an/a
Named capturing group Any capturing group with a number as its name If the name of the group is a number, that becomes the group’s name and the group’s number. (?<17>abc|def)=\17 matches abc=abc or def=def, but not abc=def or def=abc. non/an/anon/anononon/an/an/an/an/an/anon/an/an/an/an/an/an/an/a
Named backreference Any named backreference A number is a valid name for a backreference which then points to a group with that number as its name. (?<17>abc|def)=\k<17> matches abc=abc or def=def, but not abc=def or def=abc. n/an/an/an/an/an/an/an/an/an/an/an/an/an/an/an/an/a
Named capturing group Any named capturing group A negative number is a valid name for a capturing group. (?<-17>abc){3} matches abcabcabc. The group named “-17” matches abc. n/an/aerrorn/an/an/an/an/an/an/an/an/a
Named backreference Any named backreference A negative number is a valid name for a backreference which then points to a group with that negative number as its name. n/an/an/an/an/an/an/an/an/an/an/an/an/an/an/an/an/an/an/an/an/an/an/a

Writer profile pic

Steve on Apr 26, 2020 at 06:04 am


This article is contributed by Steve. If you like dEexams.com and would like to contribute, you can write your article here or mail your article to admin@deexams.com . See your article appearing on the dEexams.com main page and help others to learn.



Post Comment

Comments( 0)

×

Forgot Password

Please enter your email address below and we will send you information to change your password.