Reputation: 944
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.
Upvotes: 2
Views: 6219
Reputation: 133744
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')]
Upvotes: 3