Sajad Sajad - 1 year ago 106
Python Question

Extract sub path from url with regex

I have this url:

I am going to extract

here is my regext:

pattern = '(http[s]?:\/\/)?([^\/\s]+\/)+[^/]+[^/]+[^/]+[^/]/(?<field1>[^/]+)/'

but it's wrong!

what is my mistake?

Answer Source

You can use this regex in python code:

>>> url = ''
>>>'^https?://(?:([^/]+)/){7}', url).group(1)

([^/]+)/){7} will match 1 or more of any non-forward-slash and a / 7 times, giving us last match in captured group #1.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download