小编Sve*_*n S的帖子

条件正则表达式:仅返回一个组

我想要匹配的两种类型的网址:

(1) www.test.de/type1/12345/this-is-a-title.html
(2) www.test.de/category/another-title-oh-yes.html
Run Code Online (Sandbox Code Playgroud)

在第一种类型中,我想匹配"12345".在第二种类型中我想匹配"category/another-title-oh-yes".

这是我想出的:

(?:(?:\.de\/type1\/([\d]*)\/)|\.de\/([\S]+)\.html)
Run Code Online (Sandbox Code Playgroud)

这将返回以下内容:

对于类型(1):

Match group 1: 12345
Match group 2: 
Run Code Online (Sandbox Code Playgroud)

对于类型(2):

Match group: 
Match group 2: category/another-title-oh-yes
Run Code Online (Sandbox Code Playgroud)

如你所见,它已经很好用了.由于各种原因,我需要正则表达式只返回一个匹配组.有没有办法实现这一目标?

regex

7
推荐指数
1
解决办法
7375
查看次数

标签 统计

regex ×1