-
How does backtracking differ from back-referencing in regular expressions?
-
How does back-referencing win limitation having with backtracking or vice-versa?
How does backtracking differ from back-referencing in regular expressions? How does back-referencing win limitation
Share
Backtracking is a way for a state machine to back up and retry other matches for a regular expression. It’s something that’s pretty much internal to the regex engine.
For example, say you’re trying to match the regex
[a-z]*a, any number of lower case characters followed by ana.Given the input
abca, a greedy match will assign all of that to the[a-z]portion of the regex but then there’s no way to match the finala. Backtracking allows the engine to back up by returning that finalato the input stream and trying again, assigningabcto the[a-z]portion andato theaportion.Back-referencing on the other hand, is a means for a user of the regex engine to reference previously captured groups. For example,
may be a command to insert
_between two consecutive lower case letters at the start of each line. The\Nback-reference (whereNrepresents a number) refers back to the groups captured within().