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:

  1. http://localhost:8080/start.jsp
  2. ?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?

Was it helpful?

Solution

  1. (?: ) is syntax for a non-capturing group. As the name explains it doesn't capture its match.
  2. put alternate matching non-capturing group: (.*?(?:start|begin)\.jsp)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top