List Info

Thread: Another URL regex




Another URL regex
user name
2007-12-18 16:41:56


I need to build a Regex (POSIX BRE) to capture a parameter
from the
following string
GET /server/app.pl?
open=512&ID=233&stuff=Page&pid=29&mode=2&
;userid=U5715&cached=true&test=5&bla=xxx
HTTP/1.1


When I use the following

^.*userid=(.*)&[a-z0-9].*$

I get U5715&cached=true&test=5

But I only want U5715


What am I doing wrong?


Thanks for your help.


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Regex" group.
To post to this group, send email to regexgooglegroups.com
To unsubscribe from this group, send email to
regex-unsubscribegooglegroups.com
For more options, visit this group at http://groups.go
ogle.com/group/regex
-~----------~----~----~----~------~----~------~--~---


Re: Another URL regex
user name
2007-12-18 20:16:55
On Dec 19, 5:41 am, birkey <bir...gmail.com> wrote:
> I need to build a Regex (POSIX BRE) to capture a
parameter from the
> following string
> GET /server/app.pl?
>
open=512&ID=233&stuff=Page&pid=29&mode=2&
;userid=U5715&cached=true&test=5&bla=xxx
> HTTP/1.1
>
> When I use the following
>
> ^.*userid=(.*)&[a-z0-9].*$
>
> I get U5715&cached=true&test=5
>
> But I only want U5715
>
> What am I doing wrong?
>
> Thanks for your help.

birkey,

The problem is the '.*' after 'userid=' in your pattern. 
'.' matches
anything but a newline, and the '*' quantifier matches as
much as it
possibly can.  This means it will match to the last '&'
character in
the string, which is what you are seeing.  You might have
better luck
if you are more specific in your pattern.  You can use a
negated
bracket expression (similar to a character class in more
common
regular expression syntax) to match what can't be in a
userid:
'[^&]*'.  This will match anything that isn't a '&',
which will stop
matching when you want it to stop.

I hope this helps.

Jeff
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Regex" group.
To post to this group, send email to regexgooglegroups.com
To unsubscribe from this group, send email to
regex-unsubscribegooglegroups.com
For more options, visit this group at http://groups.go
ogle.com/group/regex
-~----------~----~----~----~------~----~------~--~---


Re: Another URL regex
user name
2007-12-18 21:10:21

That's the ticket!

I thaught that a '.*' would stop when it hit the first
literal '&'
I was wrong.


Thanks for the help


On Dec 18, 8:16 pm, Jeff <jeff.hill...gmail.com> wrote:
> On Dec 19, 5:41 am, birkey <bir...gmail.com> wrote:
>
>
>
>
>
> > I need to build a Regex (POSIX BRE) to capture a
parameter from the
> > following string
> > GET /server/app.pl?
> >
open=512&ID=233&stuff=Page&pid=29&mode=2&
;userid=U5715&cached=true&test=5&bl-a=xxx
> > HTTP/1.1
>
> > When I use the following
>
> > ^.*userid=(.*)&[a-z0-9].*$
>
> > I get U5715&cached=true&test=5
>
> > But I only want U5715
>
> > What am I doing wrong?
>
> > Thanks for your help.
>
> birkey,
>
> The problem is the '.*' after 'userid=' in your
pattern.  '.' matches
> anything but a newline, and the '*' quantifier matches
as much as it
> possibly can.  This means it will match to the last
'&' character in
> the string, which is what you are seeing.  You might
have better luck
> if you are more specific in your pattern.  You can use
a negated
> bracket expression (similar to a character class in
more common
> regular expression syntax) to match what can't be in a
userid:
> '[^&]*'.  This will match anything that isn't a
'&', which will stop
> matching when you want it to stop.
>
> I hope this helps.
>
> Jeff- Hide quoted text -
>
> - Show quoted text -
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Regex" group.
To post to this group, send email to regexgooglegroups.com
To unsubscribe from this group, send email to
regex-unsubscribegooglegroups.com
For more options, visit this group at http://groups.go
ogle.com/group/regex
-~----------~----~----~----~------~----~------~--~---


[1-3]

about | contact  Other archives ( Real Estate discussion Medical topics )