Thanks for the suggestions Art but I still can't make the regex work. I've
simplified the question to the following which I'd be grateful if anyone
could explain to me...

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

The above snippet prints '123' which is what I would hope for!

However I also want to be able to match if the string in $teststr does not
contain the trailing '&'.  I tried placing a '?' after the '&' sign in the
regex but this does not match.  i.e.

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

Can anyone tell me why this snippet does not match and print the value 123.

Bill Stennett

----- Original Message -----
From: Arthur Cohen <[EMAIL PROTECTED]>
To: Bill Stennett <[EMAIL PROTECTED]>;
<[EMAIL PROTECTED]>
Sent: Thursday, May 24, 2001 9:31 PM
Subject: RE: a regex question


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


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

Reply via email to