How to search for matching matches for a regular expression pattern in a string

I have this line

my $line = "MZEFSRGGRMEAZFE*MQZEFFMAEZF*"

and I want to find each substring, starting with Mand ending with *, and add it to the array. That means the above line will give me 6 elements in my array.

I have this code

foreach ( $line =~ m/M.*?\*/g ) {
    push @ORF, $_;
}

but it gives me only two elements in my array as it ignores overlapping rows.

Is there a way to get all matches? I tried google search but could not find the answer.

+4
source share
2 answers

It can be used code within reand Backtracking control verbsfor a little magic:

#!/usr/bin/env perl

use strict;
use warnings;

my $line = "MZEFSRGGRMEAZFE*MQZEFFMAEZF*";

local our @match;

$line =~ m/(M.*\*)(?{ push @match, $1 })(*FAIL)/;

use Data::Dump;

dd @match;

Outputs:

(
  "MZEFSRGGRMEAZFE*MQZEFFMAEZF*",
  "MZEFSRGGRMEAZFE*",
  "MEAZFE*MQZEFFMAEZF*",
  "MEAZFE*",
  "MQZEFFMAEZF*",
  "MAEZF*",
)
+4
source

, , , , ,

use strict;
use warnings 'all';
use feature 'say';

my $line = 'MZEFSRGGRMEAZFE*MQZEFFMAEZF*';

my @orf;

{
    my (@s, @e);
    push @s, $-[0] while $line =~/M/g;
    push @e, $+[0] while $line =~/\*/g;

    for my $s ( @s ) {
        for my $e ( @e ) {
            push @orf, substr $line, $s, $e-$s if $e > $s;
        }
    }
}

say for @orf;

MZEFSRGGRMEAZFE*
MZEFSRGGRMEAZFE*MQZEFFMAEZF*
MEAZFE*
MEAZFE*MQZEFFMAEZF*
MQZEFFMAEZF*
MAEZF*
+1

Source: https://habr.com/ru/post/1615915/


All Articles