java - How Regex backreference work -
java - How Regex backreference work -
i aware of fact backreference ovverride values if backtraking occurs , ovverided output new backreference.
but if take regex example:
([abc]+).*\1
then string:"abc me bca"
the output is:"abc me bca"
can explain how possible because per steps:
-[abc] matches input. -because quantifier + repeate 1 or more time , 1 time again matches b , c.then stops @ whitespace it's not either 'a', 'b' or 'c'. -.* eats input string after abc , farther goes \1(the backreference). - .* backtracking \1 fails , because .* i.e 0 or more through charecters , 1 time again + of [abc]+ backtracking. -in backtracking of [abc]+ until 'a' after removing 'b' , 'c' still no match \1 bca.
so how output came "abc me bca"..?
first character a
, lastly character a
.so match.
what happens abc
captured in grouping compared bca
.it fails.
so engine backtracks 1.now ab
compared ca
.it fails.so engine
will backtrack again.a
compared lastly a
, passes.so engine
stores a
in grouping satisfies match criteria .note \1
gets
stored in first group.it not fixed value.see demo.
https://regex101.com/r/sj9gm7/72
java regex
Comments
Post a Comment