Thanks to everyone who made suggestions.

For my purpose the regex solution is probably the easiest since I can rely
on the string being in a known format.

Using the /(?:^|::)var2=([^:]+)(?:$|::)/ approach to match the beginning /
end of a line or the '::' sequence is just the job :-)

Bill

-----Original Message-----
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] Behalf Of Bowie
Bailey
Sent: 15 June 2006 15:38
To: activeperl mailing list
Subject: RE: regex to parse name=value pairs from a string



Bill Stennett - compuserve wrote:
> Hi All,
>
> I have a string formatted like this:
>
> my $mystring = 'var1=123::var2=abc456::var3=10.99::var4=def';
>
> What I need to do is to be able to extract name=value. I cannot be
> sure where in the list a any value will occur so to extract a value
> for 'var2' from the above example would require matching either:
>
>  '::var2=abc456::'
>
> or
>  'var2=abc456::'
>
> or
>  '::var2=abc456'
>
> Can anyone suggest a regex to do this?
>
> I've been trying things like:
>
> ( $valueforvar2 ) = $mystring =~ m/var2=(.+)[::]/;
>
> but this only works if 'var2' is not last in the string. It also
> generqate a warning as follows: "POSIX syntax [: :] belongs inside
> character classes in regex; marked by <-- HERE in m/var2=(.+)[::] <--
> HERE / at Untitled line...
>
> Any suggestions appreciated.

( $valueforvar2 ) = $mystring =~ /(?:^|::)var2=([^:]+)(?:$|::)/;

--
Bowie
_______________________________________________
ActivePerl mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


_______________________________________________
ActivePerl mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to