in reply to Tracking consecutive characters

The naive way is, of course, !!+. There's also a the {MIN,MAX} syntax that lets you explicitly specify a quantity, where either of the numbers the comma and MAX value is optional, that lets you write !{2,}. Since MAX defaults to "infinite" if left out, that will match when there are at least two exclamation marks. MIN defaults to zero, btw, and If you leave out the comma as in .{2}, that sets both MIN and MAX ie it means "exactly this many".

If you put the entire expression in capturing parentheses, you get to look at the length of the captured string.

So you probably want something like m/(!{2,})/g.

Makeshifts last the longest.

Replies are listed 'Best First'.
Re^2: Tracking consecutive characters
by hv (Prior) on Sep 27, 2004 at 09:20 UTC

    Careful there: only the second number is optional. If you miss out the first number perl will fail to recognise the braces as a quantifier, and so it gets treated as a literal string to match instead:

    zen% perl -wle 'print "miss" if "aa" !~ /^a{,3}$/' miss zen% perl -wle 'print "match" if "aa" =~ /^a{0,3}$/' match zen% perl -wle 'print "match" if "a{,3}" =~ /^a{,3}$/' match zen%

    Hugo

      D'oh! I've never actually used the curlies quantifier that way, so I didn't even know (in fact I've hardly used curlies ever, period). Thanks for the note, I updated my node accordingly.

      Makeshifts last the longest.

Re^2: Tracking consecutive characters
by Jasper (Chaplain) on Sep 27, 2004 at 10:53 UTC
    The naive way is, of course, !!+

    It may be the naive way, but sometimes that's the best way (and why not in this case)

      I didn't say it wasn't. :-) In fact, with most regex engines, the star quantifier is optimized better than the plus quantifier which in turn is often optimized better than the curlies quantifier, so it may even be better to use !!!* here if performance is what you need. (Caveat benchmark etc.)

      Makeshifts last the longest.