I tried to create a regex string to analyze link
site- www.example.com/page.php?u=userid&action=add&date=yyyy-MM-dd
I want to create named groups as:
site: includes the full requested link
user: includes value of u parameter
action: includes value of action parameter
by taking example above the result will be:
site: www.example.com/page.php?u=userid&action=add&date=yyyy-MM-dd
user: userid
action: add
This regex gives you named captures of site, user and action,
(?=(?<site>www.*$))(?=.*u=(?<user>(?:[^&]*)))(?=.*action=(?<action>(?:[^&]*)))
Play here,
In[2]: from urllib.parse import parse_qs, urlparse
In[3]: url = 'www.example.com/page.php?u=userid&action=add&date=yyyy-MM-dd'
In[4]: parsed_url = urlparse(url)
In[5]: parsed_url
Out[5]: ParseResult(scheme='', netloc='', path='www.example.com/page.php', params='', query='u=userid&action=add&date=yyyy-MM-dd', fragment='')
In[6]: parsed_query = parse_qs(parsed_url.query)
In[7]: parsed_query
Out[7]: {'u': ['userid'], 'action': ['add'], 'date': ['yyyy-MM-dd']}
In[8]: {'site': url, 'user': parsed_query['u'], 'action': parsed_query['action']}
Out[8]:
{'site': 'www.example.com/page.php?u=userid&action=add&date=yyyy-MM-dd',
'user': ['userid'],
'action': ['add']}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.