Hi Bill;

 

If you put a ? after the & in your regex it will match whether or not

the & is present.

 

BUT... you might just want to use split instead of =~ to split the

string into an array containing

    VAL1=123

    VAL2=456

    etc...

 

BUT... if you're parsing the query string for a CGI app, you might just

want to use the CGI module (or even cgi-lib.pl) instead.

 

HTH,

 

--Art



-----Original Message-----

From: Bill Stennett [mailto:[EMAIL PROTECTED]]

Sent: Thursday, May 24, 2001 4:10 PM

To: [EMAIL PROTECTED]

Subject: a regex question





Hi All

 

I'm trying to create a regex to allow me to extract a value from a

string composed of name=value pairs seperated by the '&' symbol. The

problem I have is that there is no guarantee in which order the

name=value pairs will appear - the significance of this being that the

final name value pair in the string is NOT followed by the '&' symbol.

 

Example 1

my $teststr = "VAL1=123&VAL2=456&VAL3=789";

my ($val) = $teststr =~ m/VAL1=(.*?)&/;

print $val;

 

Example 1 correctly prints the value of 'VAL1'

 

=======

 

Example 2



my $teststr = "VAL2=456&VAL3=789&VAL1=123";





my ($val) = $teststr =~ m/VAL1=(.*?)&/;

 

print $val;

 

Example 2 prints nothing since the 'VAL1=123' name value pair is NOT

followed by a '&' symbol.

 

=======

 

Can anyone show me how to construct a regex which will match both cases

i.e. "VAL1=123&" and "VAL1=123"

 

Thanks in advance for any help

 

Bill Stennett



_______________________________________________
ActivePerl mailing list
[EMAIL PROTECTED]
http://listserv.ActiveState.com/mailman/listinfo/activeperl

Reply via email to