Search code examples
regexperlmultiple-matches

Perl - multiple matches on same line with alternation


I need to extract multiple matches for a string on a single line. The line looks something like this:

./staticRoutes.10.10.30_VC;./staticRoutes.10.10.40_FEEDS

I need to extract each filename and put it in some @array. The file name on the line is separated by a ;. So in the above example, i want to extract just staticRoutes.10.10.30_VC and staticRoutes.10.10.40_FEEDS

Any help greatly appreciated.

Many thanks

John


Solution

  • This would be the regex version, it would not contain the leading ./ in the result. If this is worth it, you can use this, otherwise I would prefer the split solution.

    my $s = "./staticRoutes.10.10.30_VC;./staticRoutes.10.10.40_FEEDS";
    my @res = $s =~ m~[^/]+(?=;|$)~g;
    

    This would match any character that is not a / (the [^/]+ part), before a ; or the end of the string (the (?=;|$) part)