In a message dated 6/15/2006 11:47:16 A.M. Eastern Standard Time, Williamawalters writes:
 
> hi bill --  

> In a message dated 6/15/2006 10:16:41 A.M. Eastern Standard Time, [EMAIL PROTECTED] writes:

> > 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.
> >
> > Bill
>

> maybe try something like this.   replace the shift() in second line with your
> string variable.   i have added extra optional spaces around the delimiters - often
> a good precaution.   play with various input strings until you are confident
> you're getting the result you need.  

> C:[EMAIL PROTECTED]>perl -we "use strict;
> my %params = map { split /\s*=\s*/, $_ } split /\s*::\s*/, shift;
> print qq($_ is $params{$_} \n) for keys %params"
> "  ::  var1=abc::var2 = 10.99:: var3=abc123 ::"
> var3 is abc123
> var1 is abc
> var2 is 10.99

> hth -- bill walters  
 
here's another approach.   in this one, the pattern definitions of name, value and delimiter
are factored out for (possibly) greater maintainability and the ``::'' part is completely
ignored.   this gets around the leading/trailing spaces problem of the previous
approach (try "   var1=abc   " with the previous approach to see potential problem),
but ignoring ``::'' may or may not be what you want.   anyway...  
 
C:[EMAIL PROTECTED]>perl -we "use strict;
my $name = qr/\w+/;    my $value = qr/[\w.]+/;   my $delim = qr/ (?: \s* = \s* ) /x;
my %params = $ARGV[0] =~ / ($name) $delim ($value) /gx;
print qq(<$_> is <$params{$_}> \n) for keys %params"
"     var1=abc::var2 = 10.99:: var3=abc123    foo =999::"
<var3> is <abc123>
<var1> is <abc>
<foo> is <999>
<var2> is <10.99>
 
hth -- bill walters  
 
--- Begin Message ---
hi bill --  
 
In a message dated 6/15/2006 10:16:41 A.M. Eastern Standard Time, [EMAIL PROTECTED] writes:
 
> 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.
>
> Bill
 
maybe try something like this.   replace the shift() in second line with your
string variable.   i have added extra optional spaces around the delimiters - often
a good precaution.   play with various input strings until you are confident
you're getting the result you need.  
 
C:[EMAIL PROTECTED]>perl -we "use strict;
my %params = map { split /\s*=\s*/, $_ } split /\s*::\s*/, shift;
print qq($_ is $params{$_} \n) for keys %params"
"  ::  var1=abc::var2 = 10.99:: var3=abc123 ::"
var3 is abc123
var1 is abc
var2 is 10.99
 
hth -- bill walters  
 

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

Reply via email to