Is there a way for PCRE regular expressions to count the number of occurrences of the character it encounters (n) and stop searching after it has detected n occurrences of another character (in particular { and } ).
This is for capturing code blocks (which may or may not have code blocks nested within them).
If this simplifies, the input will be a one-line string, with the only characters other than curly braces being numbers, colons, and commas. The input must pass the following criteria before the code fragments are deleted:
$regex = '%^(\\d|\\:|\\{|\\}|,)*$%';
All braces will have a matching pair and are correctly nested.
I would like to know if this can be achieved before I start writing a script to check every character in a string and count every occurrence of a bracket. Regular expressions will be much more memory friendly, as these strings can be several kilobytes in size!
Thanks, mniz.
Decision
PCRE: Lazy and Greedy at the same time (Possessive Quantifiers)
source share