jfroebe has asked for the wisdom of the Perl Monks concerning the following question:
Hi,
I have a string that I'm trying to group the items so that I'm able to use @$0 to retrieve N number of items for use elsewhere.
1 2=3 4=5 1 2=3 and 4=5
I could just put a delimiter between the groups and use split on the modified string, but I would like it as a single regex - if that is possible.
% perl -e '$str = "1 2=3 4=5"; print "$str\n"; $str =~ s/(\w+[\w\s]*=\ +w+)(?:\s)*/$1\|/g; print "$str\n"; print "Should look like: 1 2=3|4=5 +|\n"' 1 2=3 4=5 1 2=3|4=5| Should look like: 1 2=3|4=5|
Something like so. (note: nonworking code)
perl -e '$str = "1 2=3 4=5"; print "$str\n"; $str =~ m/(\w+[\w\s]*=\w+ +)(?:\s)*/g; print "$_\n" foreach @$0;' 1 2=3 4=5 1 2=3 4=5
UPDATE: The number of items is variable. i.e. "1 2 3 2 6=7 2=4 j=384923 34 43 j 3=6". Given that string I would need grouping like so:
1 2 3 2 6=7 2=4 j=384923 34 43 j 3=6
Here is the code that works:
perl -e '$str = "1 2 3 2 6=7 2=4 j=384923 34 43 j 3=6"; print "$_\n" +foreach ($str =~ m/(\w+[\w\s]*=\w+)/g);' 1 2 3 2 6=7 2=4 j=384923 34 43 j 3=6
Jason L. Froebe
No one has seen what you have seen, and until that happens, we're all going to think that you're nuts. - Jack O'Neil, Stargate SG-1
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: regex grouping
by Roy Johnson (Monsignor) on Nov 02, 2005 at 18:31 UTC | |
by jfroebe (Parson) on Nov 02, 2005 at 19:12 UTC | |
|
Re: regex grouping
by GrandFather (Saint) on Nov 02, 2005 at 18:35 UTC |