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, 2207👍, 0💬
How to write a regular expression to parse key-value entries from Windows .INI files? Here is an exa...
How to capture the Sogou web spider entries from Web log file? Here are some Web log file entries:12...
Are you having problems using regular expressions when processing text strings in your applications ...
How to capture the Soso Spider entries from Web log file? Here are some Web log file entries:127.0.0...
All credit card numbers issued by MasterCard must start with 51 through 55 and have 16 digits. For e...