Vim help says that:
\1 Matches the same string that was matched by */\1* *E65* the first sub-expression in \( and \). {not in Vi} Example: "\([a-z]\).\1" matches "ata", "ehe", "tot", etc.
It looks like the backreference can be used in search pattern. I started playing with it and I noticed behavior that I can't explain. This is my file:
<paper-input label="Input label"> Some text </paper-input>
<paper-input label="Input label"> Some text </paper-inputa>
<aza> Some text </az>
<az> Some text </az>
<az> Some text </aza>
I wanted to match the lines where the opening and closing tags are matching i.e.:
<paper-input label="Input label"> Some text </paper-input>
<az> Some text </az>
And my test regex is:
%s,<\([^ >]\+\).*<\/\1>,,gn
But this matches lines: 1, 3 and 4. Same thing with sed:
$ sed -ne 's,<\([^ >]\+\).*<\/\1>,\0,p' file
<paper-input label="Input label"> Some text </paper-input>
<aza> Some text </az>
<az> Some text </az>
This: <\([^ >]\+\) should be greedy and when trying to match it without \1 at the end then all the groups are correct. But when I add \1 it seems that <\([^ >]\+\) becomes not greedy and it tries to force the match in 3rd line. Can someone explain why it matches 3rd line:
<aza> Some text </az>
This is also a regex101 demo
NOTE This is not about the regex itself (probably there is other way to do it) but about the behavior of that regex.
\1equalsazon line three after all of the backtracking. (Since you never added anchors)<\([^ >]\+\)[ >].*<\/\1>