tags:

views:

118

answers:

2

Ok I promise I'll learn regular expressions tonight but right now I need a quick fix. (Really, I swear I will!)

How do I extract the value of what comes after name= this url:

page?id=1&name=hello

In this case I would be trying to isolate 'hello'.

Thanks!

+2  A: 

With most engines:

[\&\?]name\=(.*?)(?:&|$)

You've got it in $1.

streetpc
Joey
There is no lookahead, just a non-capturing group.
streetpc
+1  A: 

What language are you using? Pretty much every language has a utility that will do this for you so you don't have to resort to regex:

PHP:

parse_str(parse_url('page?id=1&name=hello', PHP_URL_QUERY), $query);
print $query['name']; // outputs hello

Python:

>>> from urlparse import urlparse
>>> from cgi import parse_qs
>>> parse_qs(urlparse('page?id=1&name=hello').query)
{'id': ['1'], 'name': ['hello']}
Paolo Bergantino
Yeah I know! But I'm using regex for mod_rewrite :-(
Tyler
Ah. I guess I'll leave this here anyways.
Paolo Bergantino
Thank you for answering! I always appreciate your responses.
Tyler