Backreferences match the same text as previously matched by a capturing group. Suppose you want to match a pair of opening and closing HTML tags, and the text in between. By putting the opening tag into a backreference, we can reuse the name of the tag for the closing tag. Here's how: <([A-Z][A-Z0-9]*)\b[^>]*>.*?</\1>.
To figure out the number of a particular backreference, scan the regular expression from left to right. Count the opening parentheses of all the numbered capturing group. The first parenthesis starts backreference number one, the second number two, etc. Skip parentheses that are part of other syntax such as non-capturing groups.
Read full article from Regex Tutorial - Backreferences To Match The Same Text Again
To figure out the number of a particular backreference, scan the regular expression from left to right. Count the opening parentheses of all the numbered capturing group. The first parenthesis starts backreference number one, the second number two, etc. Skip parentheses that are part of other syntax such as non-capturing groups.
Read full article from Regex Tutorial - Backreferences To Match The Same Text Again
No comments:
Post a Comment