My string looks like this: "https://google.com/bar/foobar?count=1" or it could be "https://google.com/bar/foobar"
I want to extract the value foobar
- it appears after /bar
and has an optional ?
My regex looks like this:
m = re.match(r'(.*)/bar/(.*)((\?)(.*))?', data)
When I use this regex over example 2: "https://google.com/bar/foobar"
I get two groups
('https://google.com', 'foobar', None, None, None)
When I use this regex on the first example: "https://google.com/bar/foobar?count=1"
I get
('https://google.com', 'foobar?count=3', None, None, None)
But I would like the second group to just be foobar
without the ?count=3
How would I achieve that?
My understanding so far is
(.*)/bar/(.*)((\?)(.*))?
is as follows:
(.*)
matches the first part of the string. \?
matches the ?
and ((\?)(.*))
matches ?count=3
and this is enclosed in ?
because it is supposed to be optional.