Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Repeating regex assign to a list

by iang (Sexton)
on Mar 04, 2016 at 17:56 UTC ( #1156834=perlquestion: print w/replies, xml ) Need Help??

iang has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks I have a string
$a ="aaa bbb ccc dd ee ff dd TAG=lls foo TAG=some randome tag TAG=last +_tag";
I want to use a regex to assign a portion of the string ( including whitespace) into different slots in an array there can be any number of TAG=xxxx values. I tried the regex :
@b = ($a =~ m/aaa bbb .*? (TAG=.*) */g); @b = ($a =~ m/aaa bbb .*? {1,}(TAG=.*) */g)
I used several other permutations of a regex all to no avail. I was hoping that somebody can point me in the right direction. Thank You

Replies are listed 'Best First'.
Re: Repeating regex assign to a list
by Tanktalus (Canon) on Mar 04, 2016 at 18:10 UTC

    Not entirely clear what you want... so I'm guessing a bit.

    #!/usr/bin/env perl5.22.1 use strict; use warnings; use 5.10.1; my $x ="aaa bbb ccc dd ee ff dd TAG=lls foo TAG=some randome tag TAG=l +ast_tag"; #my $x ="TAG=lls foo TAG=some randome tag TAG=last_tag"; my @tags = split /\s*TAG=/, $x; # before the first TAG, discard. shift @tags; my $i = 0; say "tag ", $i++, ": [$_]" for @tags;
    and the output is:
    tag 0: [lls foo] + tag 1: [some randome tag] tag 2: [last_tag]
    Is that what you're looking for?

      It is exactly what I was looking for, only I was trying to be a bit clever (or not) and wanted to shorten it into a single REGEX. --- thank you for your reply

        Tanktalus's split approach is probably better, but here's a "clever (or not) ... single regex" that produces the same output:

        c:\@Work\Perl\monks>perl -wMstrict -MData::Dump -le "use 5.010; ;; my $s = 'aaa bbb ccc dd ee ff dd TAG=lls foo TAG=some randome tag TAG +=last_tag'; ;; my @ra = $s =~ m{ TAG \s* = \K (?: (?! \s* TAG) .)* }xmsg; dd \@ra; " ["lls foo", "some randome tag", "last_tag"]
        (Needs 5.10+ for the  \K regex operator.)


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

Re: Repeating regex assign to a list
by neilwatson (Priest) on Mar 04, 2016 at 18:08 UTC
Re: Repeating regex assign to a list
by Laurent_R (Canon) on Mar 04, 2016 at 18:08 UTC
    You need to use parentheses in your regexes if you want to capture parts of the input string.

    Update: Oops, sorry, I misread your post, you do have some parens. But you need tell us exactly what you want to capture, an example of the input and expected output would be great. The .* greedy quantifier after the TAG= part is quite suspicious.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (10)
As of 2019-05-27 13:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you enjoy 3D movies?



    Results (156 votes). Check out past polls.

    Notices?