Backtracking of quantified subpatterns
What is backtracking of quantified subpatterns? What does it mean that a quantified subpattern is "backtracked"?
How to show that a quantified subpattern is "backtracked" on this PHP script: if ($t=10): echo "Just started...!"; endif; if ($t=11): echo "Hungry!"; endif; if ($t=12): echo ""Ah, lunch-time!"; endif;?
The backtracking behavior of quantified subpatterns is referring to the default behavior of backtracking repeated matches of a quantified subpattern to allow the rest of the pattern to match.
This default backtracking behavior is very useful in many cases. For example, the pattern /\w+day/ will have no match on "Sunday" without backtracking, because the quantified subpattern '\w+' is "greedy" by default. It will not stop after mathching "Sun" and continue to match the entire string of "Sunday". The next subpattern is 'd' and causing the entire matching process to fail, since there is nothing left to match against.
With the default backtracing behavoir, /\w+day/ will match on "Sunday", because '\w+' will first match to "Sunday", then backtrack 3 times to return 3 characters to match the rest of the pattern 'day'. In other words,
(\w+)day # matches 'Sunday' with $1='Sun', backtracked from 'Sunday'
The regular expression to show a quantified subpattern is "backtracked" on the given PHP script:
(if .+ endif;) if - matches 'if' .+ endif; - '.+' matches all characters to the end, backtracked 8 characters to match ' endif'
2013-01-26, 1955👍, 0💬
All credit card numbers issued by Diners Club must start with 300 through 305, 36 or 38 and have 14 ...
A free online regular expression test tool that allows to try you regular expression pattern and see...
How to perform a basic validation on email address using a regular expression? For example, "test+em...
Are you having problems using regular expressions when processing text strings in your applications ...
All credit card numbers issued by JCB have 3 sets of numbers: JCB cards start with 2131 have 15 digi...