正则表达式或不分组

发布于 2025-01-09 11:19:13 字数 649 浏览 3 评论 0原文

我想通过正则表达式获取 url 的线程 ID。

Url 可以具有以下状态:

  1. https://mypage.com/threads/an-example-线程/
  2. https://mypage.com/threads/an-example-thread/page-1
  3. https://mypage.com/threads/an-example-thread

我的模式 .+/threads/(.+)/.+ 涵盖了前两个选项。现在我需要一个模式,它也涵盖选项 3。.+/threads/(.+)(/.+|$) 有效。但我使用第一组来获取胎面 ID/名称。那么如何在不分组的情况下创建 or 模式呢?

I want to get the thread-id of an url via regex.

The Url can have these states:

  1. https://mypage.com/threads/an-example-thread/
  2. https://mypage.com/threads/an-example-thread/page-1
  3. https://mypage.com/threads/an-example-thread

My pattern .+/threads/(.+)/.+ covers the first two options. Now I need a pattern, that also covers option 3. .+/threads/(.+)(/.+|$) works. But I use the first group to get the tread-id/name. So how is is possible to create an or-pattern without grouping?

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

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

发布评论

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

评论(1

红衣飘飘貌似仙 2025-01-16 11:19:13

正如评论中提到的,尝试使用 /threads/([^\/]*),它将匹配所有 3 个。

As mentioned in the comments, try to use /threads/([^\/]*), that will match all 3.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文