Python regular expression to get URL - codesd.com
source link: https://www.codesd.com/item/python-regular-expression-to-get-url.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Python regular expression to get URL
I am trying to get a URL out of a long string and I am unsure how write the regex;
$ string = '192.00.00.00 - WWW.WEBSITE.COM GET /random/url/link'
I am trying to use the 're.search' function in order to pull out the WWW.WEBSITE.COM only without spaces. I would like it look like this;
$ get_site = re.search(regex).group()
$ print get_site
$ WWW.WEBSITE.COM
BUT they will all be in between a (-) and the (GET)
That is all the information you need:
>>> import re
>>> string = '192.00.00.00 - WWW.WEBSITE.COM GET /random/url/link'
>>> re.search('-\s+(.+?)\s+GET', string).group(1)
'WWW.WEBSITE.COM'
>>>
Below is a breakdown of what the Regex pattern is matching:
- # -
\s+ # One or more spaces
(.+?) # A capture group for one or more characters
\s+ # One or more spaces
GET # GET
Note too that .group(1)
gets the text captured by (.+?)
. .group()
would return the entire match:
>>> re.search('-\s+(.+?)\s+GET', string).group()
'- WWW.WEBSITE.COM GET'
>>>
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK