正则表达式或不分组
我想通过正则表达式获取 url 的线程 ID。
Url 可以具有以下状态:
- https://mypage.com/threads/an-example-线程/
- https://mypage.com/threads/an-example-thread/page-1
- 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:
- https://mypage.com/threads/an-example-thread/
- https://mypage.com/threads/an-example-thread/page-1
- 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如评论中提到的,尝试使用
/threads/([^\/]*)
,它将匹配所有 3 个。As mentioned in the comments, try to use
/threads/([^\/]*)
, that will match all 3.