in reply to Regex catching multiple characters next to each other

If you're interested in the count, try this approach:

#!/usr/bin/perl use v5.14; $_ = 'LLL'; my @matches = m/(L)(?=L)/g; say scalar @matches; #prints 2, as expected

The (L)(?=L) expression looks for every 'L' immediately followed by another 'L'. As there are two such matches, the @matches array contains two elements.

regards,
Luke Jefferson

Replies are listed 'Best First'.
Re^2: Regex catching multiple characters next to each other
by AnomalousMonk (Archbishop) on Oct 06, 2014 at 13:04 UTC
    use v5.14;

    NB: Perl version 5.14 is needed only for say. The regexery works fine under 5.8.9 (tested) and I think it would work as well under 5.0, but I can't test back that far and I'm too lazy to check the docs.

      It sure works on 5.8.8 that ships with my AIX. Still, I have at least two other reasons for using the 'use 5.14;' line: it's a good shortcut for 'use strict;', and it's an expression of my firm belief that we should try to promote using modern versions of Perl as often as possible.

      But you are right in making it clear that if someone wants to use this approach, he should not be discouraged by the version statement I used. I should have added this to my reply. Thank you.

      Luke Jefferson