0

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!

1
  • It depends on the language. What language and RegEx client are you using? Commented Jun 29, 2009 at 18:53

2 Answers 2

3

With most engines:

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

You've got it in $1.

Sign up to request clarification or add additional context in comments.

2 Comments

You don't need to escape & and ? inside character classes, = doesn't need escaping at all and the part after the = could probably be written as ([^&]*) to avoid the lookahead.
There is no lookahead, just a non-capturing group.
1

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

3 Comments

Yeah I know! But I'm using regex for mod_rewrite :-(
Ah. I guess I'll leave this here anyways.
Thank you for answering! I always appreciate your responses.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.