Disclosure: very much a regex newbie, so I'm trying to tweak some example code I found which parses web server log data into named groups. The snippet of my modified regex thus far that deals with the URL and query string groups:
(?P<url>.+)(?P<querystr>\?.*)
This works just fine when the string against which it's applied actually does have a query string on the URL (each group gets the expected bit of the string) but fails to match if there is none. So I tried adding a '?' after the "querystr" group to indicate that it was optional, i.e. (?P<querystr>\?.*)? ... if there's no query string then it works as expected (nothing is extracted into querystr), but when there is one, it is still extracted as part of url rather than separately into querystr.
What's the best way to identify optional groups (assuming that's even the right approach in this case)? Thanks in advance.
^(?P<url>.+?)(?P<querystr>\?.*)?$or^(?P<url>[^?]+)(?P<querystr>\?.*)?$