Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Please help with regexp - finding all matches?

Reply
Thread Tools

Please help with regexp - finding all matches?

 
 
Boris Pelakh
Guest
Posts: n/a
 
      04-08-2004
I'm trying to extract name:value pairs from a string similar to this one

{a:b,c:d,e:f}

The individual pieces are more complicated, but its similar in principal

Here is a test script I use

$f = "a:b,c:d,e:f";
@a = ($f =~ m/(?\w+)\w+))(?:,(?\w+)\w+)))+/);
print "@a\n";

I should be seeing "a b c d e f", but I am only getting "a b e f",
i.e. the first and last group matched. Why am I not seeing the rest?
Ideally I want to just assign the result of the match to a hash.

-- Boris
 
Reply With Quote
 
 
 
 
Jürgen Exner
Guest
Posts: n/a
 
      04-08-2004
Boris Pelakh wrote:
> Here is a test script I use
>
> $f = "a:b,c:d,e:f";
> @a = ($f =~ m/(?\w+)\w+))(?:,(?\w+)\w+)))+/);
> print "@a\n";
>
> I should be seeing "a b c d e f", but I am only getting "a b e f",


What about a simple

use strict;
use warnings;
my $f = "a:b,c:d,e:f";
my @a;
for (split /,/, $f) {
push @a ,(split /:/);
}
print "@a\n";

In my opinion much easier to write and to read than a complicated RE.

jue


 
Reply With Quote
 
 
 
 
Jim Gibson
Guest
Posts: n/a
 
      04-08-2004
In article <(E-Mail Removed) >, Boris
Pelakh <(E-Mail Removed)> wrote:

> I'm trying to extract name:value pairs from a string similar to this one
>
> {a:b,c:d,e:f}
>
> The individual pieces are more complicated, but its similar in principal
>
> Here is a test script I use
>
> $f = "a:b,c:d,e:f";
> @a = ($f =~ m/(?\w+)\w+))(?:,(?\w+)\w+)))+/);
> print "@a\n";
>
> I should be seeing "a b c d e f", but I am only getting "a b e f",
> i.e. the first and last group matched. Why am I not seeing the rest?

You only see "a b e f" in your output because you only have 4 capturing
parentheses in your pattern, and you only apply your pattern once. The
entire string is matched due to the '+' after the second subgrouping,
but the capturing bits in this group are applied twice: first to
capture the 'c' and the 'd', and then to match and capture the 'e' and
the 'f', overwriting the 'c' and 'd'.

If you want all of the matching bits, use a simpler pattern but use it
repeatedly with the 'g' modifier:

my @a = ( $f =~ m/(?\w+)\w+))/g );

but, as Jürgen pointed out, using split twice is simpler.

> Ideally I want to just assign the result of the match to a hash.


Well, then, assign the results of the match to a hash:

my %h = ( $f =~ m/(?\w+)\w+))/g );

FYI: this newsgroup is defunct. Try comp.lang.perl.misc in the future.
 
Reply With Quote
 
Purl Gurl
Guest
Posts: n/a
 
      04-08-2004
Boris Pelakh wrote:

(snipped)

> I'm trying to extract name:value pairs


> $f = "a:b,c:d,e:f";
> @a = ($f =~ m/(?\w+)\w+))(?:,(?\w+)\w+)))+/);


> I should be seeing "a b c d e f", but I am only getting "a b e f",



#!perl

$f = "a:b,c:d,e:f";

@a = split (/,/, $f);

print "@a";


Purl Gurl
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[regexp] How to convert string "/regexp/i" to /regexp/i - ? Joao Silva Ruby 16 08-21-2009 05:52 PM
Ruby 1.9 - ArgumentError: incompatible encoding regexp match(US-ASCII regexp with ISO-2022-JP string) Mikel Lindsaar Ruby 0 03-31-2008 10:27 AM
Programmatically turning a Regexp into an anchored Regexp Greg Hurrell Ruby 4 02-14-2007 06:56 PM
RegExp.exec() returns null when there is a match - a JavaScript RegExp bug? Uldis Bojars Javascript 2 12-17-2006 09:59 PM
please help... ...me learn C++ please please please :) KK C++ 2 10-14-2003 02:08 PM



Advertisments