in reply to how to extract multiple pattern into array

roboticus and toolic are right that you should be using a real parser for such relatively complex input. Why try to re-implement a parser when it's already been done for you by someone else? :-)

Just for completeness, how to fix your script: 1. You use local $/ = "";, which will cause the entire file to be slurped on the first iteration of the while loop, and then you only call your regex once. If you want to match once per line, remove the local $/ = "";, otherwise, you could use something like push @fields, $1 while /$reM/g; to match as many times as possible in the current string. Also, I'd change .* to being non-greedy, i.e. .*?. 2. Your regex requires a space after the opening {, but your input does not have a space there, so it will not match. I'd suggest something like \s* to match any amount of whitespace, even none.

Replies are listed 'Best First'.
Re^2: how to extract multiple pattern into array
by AnomalousMonk (Archbishop) on Feb 02, 2015 at 18:54 UTC
    ... local $/ = ""; ... will cause the entire file to be slurped ...

    From  perldoc -v "$/" (see also perlvar):

    HANDLE->input_record_separator( EXPR ) $INPUT_RECORD_SEPARATOR $RS $/ The input record separator ... treating empty lines as a terminator if set to the null string + ... You may set it to ... "undef" to read through the end of file. ...
    The idiomatic Perl statement would be something like
        my $entire_data = do { local $/;  <$filehandle>; };
    (if you don't use something like File::Slurp).


    Give a man a fish:  <%-(-(-(-<

      Oops, thanks :-)

        I had finished my code , thank you for your help!
        #!/usr/bin/perl use 5.010; use strict; use warnings; my $reS = qr{\..*?\s*?\(\s*?([0-9a-zA-Z_]*?)\s*?\)}s; my $reM = qr{\..*?\s*?\(\s*?\{\s*?(.*?)\s*?\}\s*?\)}s; open my $fh, "analog_atop_inst.v" or die $!; my @fields=(); my @matchM=(); my @matchS=(); my @allfield=(); my $entire_dataS = ''; my $entire_dataM = ''; while (<$fh>) { chomp; $entire_dataS .= $_; $entire_dataM .= $_; } #print $entire_data; while (1) { if ($entire_dataS =~ /$reS/){ push @matchS, $1; $entire_dataS = $';} else { last;} } while (1) { if ($entire_dataM =~ /$reM/){ push @matchM, $1; $entire_dataM = $';} else { last;} } #say "@@@@@@@@@@@@ MatchS @@@@@@@@@@@@@@@@@"; foreach my $fieldS (@matchS){ push @allfield, $fieldS ; } #say "############ MatchM #################"; foreach my $fieldM (@matchM){ my @fieldsplit = split / ,\s+/, $fieldM; foreach my $fieldMs (@fieldsplit){ push @allfield, $fieldMs ; } } foreach my $fieldT (@allfield){ say $fieldT; }