Search code examples
regexregex-group

Regex or without grouping


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?


Solution

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