Tyler
Tyler

Reputation: 4709

capture part of url with regex

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!

Upvotes: 0

Views: 428

Answers (2)

Paolo Bergantino
Paolo Bergantino

Reputation: 488384

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']}

Upvotes: 1

instanceof me
instanceof me

Reputation: 39138

With most engines:

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

You've got it in $1.

Upvotes: 3

Related Questions