(?:\;jsessionid=[^\?#]*) 怎么样?在正则表达式工作中?

发布于 2024-10-11 06:46:03 字数 655 浏览 0 评论 0原文

假设我要匹配以下文本:

http://localhost:8080/start.jsp;jsessionid=9E4CDB636248C9610F57704E5E07F782?whatever=true&somethingelse=true

使用此正则表达式:

^(.*?start\.jsp)(?:\;jsessionid=[^\?#]*)?(\?[^#]*)?(#.*)?$

结果组为:

  1. http://localhost:8080/start.jsp
  2. ?whatever=true&somethingelse=true

A. 为什么组号 2 不是这样的:;jsessionid=9E4CDB636248C9610F57704E5E07F782
第二组开头的这部分 ?:\ 有何作用?

B. 另外,如果我的选项是 begin.jspstart.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:

  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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

神经大条 2024-10-18 06:46:03
  1. (?: ) 是非捕获组的语法。正如名称所解释的,它不会捕获其匹配项。
  2. 放置备用匹配的非捕获组:(.*?(?:start|begin)\.jsp)
  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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文