Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

array of strings that matched a pattern

by Anonymous Monk
on Apr 29, 2011 at 12:56 UTC ( #901974=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks

In pattern matching there are memory variables to store the results of the match so as $1, $2 etc. Is there an array variable containing all of the individual matched components so that I can join them into a tab delimted string conveniently?

many thanks

Comment on array of strings that matched a pattern
Re: array of strings that matched a pattern
by Tanktalus (Canon) on Apr 29, 2011 at 13:17 UTC

    If you call it right, yes.

    if (my @matches = $text =~ /some (regex) here (with) lots (of) capture +s/) { print join "\t", @matches; }
      hi, that crashes for me (eclipse IDE goes black and does not respond). i'm in a while loop as shown in pseudocode
      while (<infile>) { if (my @matches = $_ =~ /exp/) { print join "\t", @matches; } }
      i had to do this
      while (<infile>) { if (my @matches = $_ =~ /exp/) { my $line = join "\t", @matches; print $line; } }
      I can't see what was wrong with the first version?
        Buffering?

        Throw a print "\n"; after the print join and see if it works (you probably didn't want the results from each line butted up against each other anyhow)

        You may want to set $|=1 at the start too, if you haven't already.

        I'm looking at Pro Perl by Peter Wainwright, and it indicates that:

        1) setting $| or

        2) using the autoflush method, if using IO::File or IO::Handle or

        3) printing to a terminal (not sure how perl sees your IDE)

        turns off block buffering, but line buffering is still active. I'd think terminating the line should guarantee a flush.

        -Greg

Re: array of strings that matched a pattern
by LanX (Canon) on Apr 29, 2011 at 13:17 UTC
    the match operator m// returns all groupings in list context.

    DB<101> $_ = join ",","a".."z" DB<102> @matches = /(\w)/g DB<103> p @matches abcdefghijklmnopqrstuvwxyz DB<104> @matches = /(\w),(\w)/ DB<105> p @matches ab DB<106> $a = join ",","a".."z" DB<107> @matches = ( $a =~ /(\w)/g ) # parens just for clarificat +ion DB<108> p @matches abcdefghijklmnopqrstuvwxyz

    Cheers Rolf

    UPDATE: extended examples

Re: array of strings that matched a pattern
by toolic (Bishop) on Apr 29, 2011 at 13:20 UTC
    This is probably not what you're looking for, but this will grab all matches for one regex into an array:
    use warnings; use strict; use Data::Dumper; my $s = 'a b c d'; my @letters; if (@letters = $s =~ /(\w)/g) { print Dumper(\@letters); } __END__ $VAR1 = [ 'a', 'b', 'c', 'd' ];

    See perlre and Perl Idioms Explained - @ary = $str =~ m/(stuff)/g

    Can you show an example of your input string and your regex?

    Update: If you use perl 5.10, you could use named captures and the %- hash.

    use warnings; use strict; use Data::Dumper; if ('1234' =~ /(?<A>1)(?<B>2)(?<A>3)(?<B>4)/) { print Dumper(\%-); } __END__ $VAR1 = { 'A' => [ '1', '3' ], 'B' => [ '2', '4' ] };
Re: array of strings that matched a pattern
by Fletch (Chancellor) on Apr 29, 2011 at 13:23 UTC

    Slightly tangental but just for edification's sake: there's also @+ and @- which store offsets into the string and the documentation for the later gives examples to translate a match variable $n into a corresponding substr($str, $-[n], $+[n]-$-[n]) call.

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

Re: array of strings that matched a pattern
by jwkrahn (Monsignor) on Apr 29, 2011 at 13:28 UTC

    You can do that without a variable:

    my $string = join "\t", $text =~ /Key:\s*(\S+)\s+(\S+)\s+(\S+)\s+(\S+) +/;
Re: array of strings that matched a pattern
by ww (Bishop) on Apr 29, 2011 at 15:21 UTC

    Some variations, just for fun...

    #!/usr/bin/perl use strict; use warnings; use 5.012; # 901974 say ">\t Iteration 1"; my $text = "some regex here with lots of captures"; say "\$text: $text"; if ( my @matches = $text =~ /some (regex) here (with) lots (of) captur +es/i ) { say join "\t", @matches; } say "\n>\t Next, testing \$text1 in Iteration 1ALT"; my $text1 = "George's regex is an example of writing sample text with +an excess of complications"; # NB O +rder retained say "\$text1: $text1"; if ( my @matchesALT = $text1 =~ /.*?(regex).*?(with).*?(of).*?/i ) { say join "\t", @matchesALT; } else { say "\t No matchesALT"; } say "\n>\t Next, testing \$text2 in Iteration 1ALT"; my $text2 = "regex captures with many different matches are a horse of + another color than a regex with a single match."; # Multipl +e cases for 2 matches say $text2; if ( my @matches2 = $text2 =~ /.*?(regex).*?(with).*?(of).*?/ig ) { say "Iteration 2"; say join "\t", @matches2; say "\n>\t Next, 2a with the same \$text2"; } if ( my @matches2a = $text2 =~ /(?:regex)|(?:with)|(?:of)/ig ) { say join "\t", @matches2a; say "\n>\t Next, testing \$TextALT in Iteration 2ALT"; } my $textALT = "The result of the example when text is written with reg +ex order changed is different"; say "\$textALT: $textALT"; if ( my @matchesALT = $textALT =~ /.*?(Regex).*?(with).*?(of).*?/ig ) +{ say join "\t", @matchesALT; } else { say "Iteration 2ALT had no matches (because the order failed)" +; } say "\n>\t Iteration 3 is next"; my $text3 = "regex captures with many different matches are a horse of + another color than a regex with a single match."; say "\$text3: $text3"; if ( my @matches3 = $text3 =~ /(Regex) captures (with) many different +matches are a horse (of) another color than a (regex) (with) a single + match\./ig ) { say "Iteration 3"; say join "\t", @matches3; } else { say "No matches in iteration 3"; }

    OUTPUT

    > Iteration 1 $text: some regex here with lots of captures regex with of > Next, testing $text1 in Iteration 1ALT $text1: George's regex is an example of writing sample text with an ex +cess of complications regex with of > Next, testing $text2 in Iteration 1ALT regex captures with many different matches are a horse of another colo +r than a regex with a single match. Iteration 2 regex with of > Next, 2a with the same $text2 regex with of regex with > Next, testing $TextALT in Iteration 2ALT $textALT: The result of the example when text is written with regex or +der changed is different Iteration 2ALT had no matches (because the order failed) > Iteration 3 is next $text3: regex captures with many different matches are a horse of anot +her color than a regex with a single match. Iteration 3 regex with of regex with

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://901974]
Approved by toolic
Front-paged by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (13)
As of 2015-07-01 21:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (22 votes), past polls