문제

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?

도움이 되었습니까?

해결책

  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)
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top