Sajad Sajad - 5 months ago 33
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?


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.