How does (?:\;jsessionid=[^\?#]*)? in a regular expression work?
-
09-10-2019 - |
Question
Suppose I have this text to match:
http://localhost:8080/start.jsp;jsessionid=9E4CDB636248C9610F57704E5E07F782?whatever=true&somethingelse=true
Using this regular expression:
^(.*?start\.jsp)(?:\;jsessionid=[^\?#]*)?(\?[^#]*)?(#.*)?$
The resulting groups are:
http://localhost:8080/start.jsp
?whatever=true&somethingelse=true
A. Why isn't group number 2 this: ;jsessionid=9E4CDB636248C9610F57704E5E07F782
?
What does this part ?:\
at the beginning of second group do?
B. And also, how can I create an expression to extract the same groups as for the example above, if my options are begin.jsp
and start.jsp
(not just start.jsp
) before the jsessionid
part?
Solution
(?: )
is syntax for a non-capturing group. As the name explains it doesn't capture its match.- put alternate matching non-capturing group:
(.*?(?:start|begin)\.jsp)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow