(?:\;jsessionid=[^\?#]*) 怎么样?在正则表达式工作中?
假设我要匹配以下文本:
http://localhost:8080/start.jsp;jsessionid=9E4CDB636248C9610F57704E5E07F782?whatever=true&somethingelse=true
使用此正则表达式:
^(.*?start\.jsp)(?:\;jsessionid=[^\?#]*)?(\?[^#]*)?(#.*)?$
结果组为:
http://localhost:8080/start.jsp
?whatever=true&somethingelse=true
A. 为什么组号 2 不是这样的:;jsessionid=9E4CDB636248C9610F57704E5E07F782
?
第二组开头的这部分 ?:\
有何作用?
B. 另外,如果我的选项是 begin.jsp
和 start.jsp
(不仅仅是start.jsp
)在 jsessionid
部分之前?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
(?: )
是非捕获组的语法。正如名称所解释的,它不会捕获其匹配项。(.*?(?:start|begin)\.jsp)
(?: )
is syntax for a non-capturing group. As the name explains it doesn't capture its match.(.*?(?:start|begin)\.jsp)