Search code examples
pythonregexpython-2.7preg-matchurlparse

Regular expression in python with urlParse


I have this URL:

https://www.yoursite.com/drive/team-real-431/pepe-ozil-R323/anyway-jim-james-hi-bye-hi-321312/;jsessionid=DBDE454034B0EE325FC100112EF2E123.56AC29295781342F53AB242D03EE33

i want to have

DBDE454034B0EE325FC100112EF2E123.56AC29295781342F53AB242D03EE33

i tried

ulrJS = "https://www.yoursite.com/drive/team-real-431/pepe-ozil-R323/anyway-jim-james-hi-bye-hi-321312/;jsessionid=DBDE454034B0EE325FC100112EF2E123.56AC29295781342F53AB242D03EE33"
ulrJS = ulrJS.split('/')[-1]

Solution

  • You can use urlparse:

    >>> import urlparse
    >>> url = 'https://www.yoursite.com/drive/team-real-431/pepe-ozil-R323/anyway-jim-james-hi-bye-hi-321312/;jsessionid=DBDE454034B0EE325FC100112EF2E123.56AC29295781342F53AB242D03EE33'
    >>> url_parts = urlparse.urlparse(url)
    >>> jsessionid = dict(urlparse.parse_qsl(url_parts.params)).get('jsessionid')
    >>> print(jsessionid)
    DBDE454034B0EE325FC100112EF2E123.56AC29295781342F53AB242D03EE33