Regex将匹配不跟随任何内容的字符串

时间:2022-03-20 20:13:22

I am trying to figure out a regex sequence that will match the first item in the list below but not the other two, {Some-Folder} is variable.

我正在尝试找出一个regex序列,它将匹配下面列表中的第一个项,而不是另外两个,{Some-Folder}是变量。

http://www.url.com/{Some-Folder}/
http://www.url.com/{Some-Folder}/thing/key/
http://www.url.com/{Some-Folder}/thing/119487302/

http://www.url.com/{Some-Folder}/{something-else}

Essentially I want to be able to detect anything that is of the form:

本质上,我想要能够检测到任何形式的东西:

http://www.url.com/{Some-Folder}/

or

http://www.url.com/{Some-Folder}

but not

但不是

http://www.url.com/{Some-Folder}/{something-else}

So far I have

到目前为止我有

http://www.url.com/[A-Z,-]*\/^.

but this doesn't match anything

但这和什么都不匹配

4 个解决方案

#1


2  

http://www.url.com/[^/]+/?$

Or, in the few parsers that use \Z as end of text,

或者,在少数使用\Z作为文本结尾的解析器中,

http://www.url.com/[^/]+/?\Z

#2


1  

I customized a regex I've used for URL parsing before, it's not perfect, and will need even more work once gTLD becomes more used. Anyway, here it is:

我定制了一个以前用于URL解析的regex,它并不完美,一旦gTLD得到更多的使用,它将需要更多的工作。无论如何,这里是:

\bhttps?:\/\/[a-z0-9.-]+\.(?:[a-z]{2,4}|museum|travel)\/[^\/\s]+(?:\/\b)?

You may want to add case insensitive flag, for whichever language you're using.

您可能想要添加大小写不敏感的标志,无论您使用哪种语言。

Demo: http://rubular.com/r/HyVXU30Hvp

演示:http://rubular.com/r/HyVXU30Hvp

#3


0  

You may use the following regex:
(?m)http:\/\/www\.example\.com\/[^\/]+\/?$

你可以使用下面的正则表达式:(?)http:\ / \ / www \ .example \ com \[/ ^ \]+ \ / ?美元

Explanation:

解释:

  • (?m) : Set the m modifier which makes ^ and $ match start and end of line respectively
  • (?):设置m修饰语使^和$比赛开始和结束时,分别
  • http:\/\/www\.example\.com\/ : match http://www.example.com/
  • http:\ / \ / www \ .example \ com \ /:http://www.example.com/相匹配
  • [^\/]+ : match anything except / one or more times
  • [/ ^ \]+:除了/一次或多次匹配
  • \/? : optionally match /
  • \ / ?:选择匹配/
  • $ : declare end of line
  • $:声明行尾。

Online demo

在线演示

#4


0  

I've been looking for an answer to this exact problem. aaaaaa123456789's answer almost worked for me. But the $ and \Z didn't work. My solution is:

我一直在寻找这个问题的答案。aaaa123456789的答案几乎对我起作用。但是,美元和美元都没有起作用。我的解决方案是:

http://www.url.com/[^/]+/?.{0}

#1


2  

http://www.url.com/[^/]+/?$

Or, in the few parsers that use \Z as end of text,

或者,在少数使用\Z作为文本结尾的解析器中,

http://www.url.com/[^/]+/?\Z

#2


1  

I customized a regex I've used for URL parsing before, it's not perfect, and will need even more work once gTLD becomes more used. Anyway, here it is:

我定制了一个以前用于URL解析的regex,它并不完美,一旦gTLD得到更多的使用,它将需要更多的工作。无论如何,这里是:

\bhttps?:\/\/[a-z0-9.-]+\.(?:[a-z]{2,4}|museum|travel)\/[^\/\s]+(?:\/\b)?

You may want to add case insensitive flag, for whichever language you're using.

您可能想要添加大小写不敏感的标志,无论您使用哪种语言。

Demo: http://rubular.com/r/HyVXU30Hvp

演示:http://rubular.com/r/HyVXU30Hvp

#3


0  

You may use the following regex:
(?m)http:\/\/www\.example\.com\/[^\/]+\/?$

你可以使用下面的正则表达式:(?)http:\ / \ / www \ .example \ com \[/ ^ \]+ \ / ?美元

Explanation:

解释:

  • (?m) : Set the m modifier which makes ^ and $ match start and end of line respectively
  • (?):设置m修饰语使^和$比赛开始和结束时,分别
  • http:\/\/www\.example\.com\/ : match http://www.example.com/
  • http:\ / \ / www \ .example \ com \ /:http://www.example.com/相匹配
  • [^\/]+ : match anything except / one or more times
  • [/ ^ \]+:除了/一次或多次匹配
  • \/? : optionally match /
  • \ / ?:选择匹配/
  • $ : declare end of line
  • $:声明行尾。

Online demo

在线演示

#4


0  

I've been looking for an answer to this exact problem. aaaaaa123456789's answer almost worked for me. But the $ and \Z didn't work. My solution is:

我一直在寻找这个问题的答案。aaaa123456789的答案几乎对我起作用。但是,美元和美元都没有起作用。我的解决方案是:

http://www.url.com/[^/]+/?.{0}