Link to Java RegEx reverse

What does it mean to have \numberin regular expression in java.

Say I have something like \1or \2. What does this mean and how is it used? An example would be very helpful.

thank

+4
source share
2 answers

Backlinks correspond to the same text as the previous capture group. Suppose you want to combine a couple of opening and closing HTML tags and text between them. By placing the opening tag in the backreference, we can reuse the tag name to close the tag. Here's how:

<([A-Z][A-Z0-9]*)\b[^>]*>.*?</\1>

This regex contains only one pair of parentheses that fix the string matches

[A-Z][A-Z0-9]*

\1 ( ) . \1 ​​ . /, . HTML , .

: http://www.regular-expressions.info/backref.html

+5

\ . escape-.

0

Source: https://habr.com/ru/post/1540275/


All Articles