Java / PHP / Python
Get both a consistent group with index 1 using both Negative Lookahead and Positive Lookbehind.
((?<=\.de\/type1\/)\d+|(?<=\.de\/)(?!type1)[^\.]+)
There are two regex patterns that are ORed.
The first regular expression pattern is looking for 12345
The second regex pattern looks for category/another-title-oh-yes .
Note:
Here's an online demo of regex101
Input:
www.test.de/type1/12345/this-is-a-title.html www.test.de/category/another-title-oh-yes.html
Output:
MATCH 1 1. [18-23] `12345` MATCH 2 1. [57-86] `category/another-title-oh-yes`
Javascript
try this and get as a consistent group at index 2.
((?:\.de\/type1\/)(\d+)|(?:\.de\/)(?!type1)([^\.]+))
Here's an online demo of regex101 .
Input:
www.test.de/type1/12345/this-is-a-title.html www.test.de/category/another-title-oh-yes.html
Output:
MATCH 1 1. `.de/type1/12345` 2. `12345` MATCH 2 1. `.de/category/another-title-oh-yes` 2. `category/another-title-oh-yes`
source share