Other Resources:
"non-greedy" on quantified subpatterns +? *? {}?
How turn off the the greediness of quantified subpatterns? I want the quantified subpattern to be matched the minimum number of times possible?
How to create a "non-greedy" quantified subpattern 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;?
✍: FYIcenter.com
![]()
You can turn off the "greedy" behavior of a quantified subpattern by adding '?' immediately after the quantifier. This will force the quantified subpattern to be matched the minimum number of times possible.
For example: when applying pattern /a+?a/ to "aaaa", the quantified subpattern /a+?/ will match the first character "a" followed by the next subpattern /a/ to match the second character "a". In other words,
(a+?)a # matches 'aaaa' with $1 for first 'a', $2 for second 'a'
Another example:
(.+?). # matches 'abcd' with $1 for 'a', $2 for 'c'
The regular expression to show a "non-greedy" quantified subpattern on the given PHP script:
(if .+? endif;)
if - matches 'if'
.+? endif; - matches the minimum number of s possible
while 'end if;' is at the end
2013-01-26, 0👍, 0💬
Popular Posts:
All credit card numbers issued by JCB have 3 sets of numbers: JCB cards start with 2131 have 15 digi...
How to write a regular expression to parse key-value entries from Windows .INI files? Here is an exa...
How to capture the MSN (Microsoft Network) bot entries from Web log file? Here are some Web log file...
How to write a regular expression to validate GUID (Globally Unique IDentifier) or UUID (Universally...
All credit card numbers issued by Diners Club must start with 300 through 305, 36 or 38 and have 14 ...