2

I want to get the query name and values to be displayed from a URL. For example, url='http://host:port_num/file/path/file1.html?query1=value1&query2=value2'

From this, parse the query names and its values and to print it.

1

2 Answers 2

8

Don't use a regex! Use urlparse.

>>> import urlparse
>>> urlparse.parse_qs(urlparse.urlparse(url).query)
{'query2': ['value2'], 'query1': ['value1']}
Sign up to request clarification or add additional context in comments.

3 Comments

And in one line urlparse.parse_qs(urlparse.urlparse("http://www.example.com:8080/abcd/dir/file1.html?query1=value1&query2=value2").query)
or [pair.split("=") for pair in urlparse(url).query.split("&")]
That may have to be updated for Python 3.
3

I agree that it's best not to use a regular expression and better to use urlparse, but here is my regular expression.

Classes like urlparse were developed specifically to handle all URLs efficiently and are much more reliable than a regular expression is, so make use of them if you can.

>>> x = 'http://www.example.com:8080/abcd/dir/file1.html?query1=value1&query2=value2'
>>> query_pattern='(query\d+)=(\w+)'
>>> # query_pattern='(\w+)=(\w+)'    a more general pattern
>>> re.findall(query_pattern, x)
[('query1', 'value1'), ('query2', 'value2')]

8 Comments

It might be worth elaborating on why regex is the wrong hammer for this nail.
Alright I think I explained it very briefly. Feel free to explain it better if you want :D
thank you jamylak. can you plase tel me how can v split it generically. for eg. if the query contains "name=asd&name1=qwerty", for this the above pattern ll not work. so instead of using name wat v can use in the query pattern. Since i'm new to python regex i'm asking this :)
See the commented out code, query_pattern='(\w+)=(\w*)'. That should work for any query.
oh sorry i haven't seen it sorry
|

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.