RegExp匹配网址

时间:2016-11-15 15:17:11

标签: regex

我有当前的网址:

ristoranti/location/latvia/riga/other-tag

如果有location

,我需要一个没有获取网址的正则表达式

这是我试过的:

ristoranti/(?!location$).*)?(.+?)/(.+?)

我需要获取的示例网址:

ristoranti/latvia/riga/other-tag

我对regexp并不是很好,但如果我是对的,那么第一段应该得到location以外的全部,我错了吗?

1 个答案:

答案 0 :(得分:0)

问题是,您的否定前瞻中存在$,无法阻止ristoranti/location/latvia/riga/other-tag匹配,因为您的网址并非真正以location结尾。你应该用

替换它
(?!location/)

当网址提前location/时,匹配将失败。

一开始也使用^。所以你的最终正则表达式应该是:

^ristoranti/(?!location/)([^/]*)/([^/]*)/(.*)

RegEx Demo

相关问题