简体   繁体   English

做正则表达式全部或不做

[英]make regular expression all or nothing

so I have this regex: 所以我有这个正则表达式:

word1\/.+?\/word2($|[^\/]*)

I want this to match 我想要这个搭配

word/2342/word2

But NOT 但不是

word/2342/word2/lalallaa

IE. IE浏览器 as soon as word2 is followed by a slash, do not match AT ALL word2后面紧跟斜杠时,请完全不匹配

Nonetheless, it would still match the 'word/2342/word2' part of the second regex... I want it to not match anything at all 尽管如此,它仍然会与第二个正则表达式的'word / 2342 / word2'部分匹配...我希望它完全不匹配任何内容

How do I fix this regex? 如何修复此正则表达式?

What about: 关于什么:

word\/.+?\/word2$

?

Replace ($|[^\\/]*) with (?!\\/) , or at least ($|[^\\/]) . ($|[^\\/]*)替换为(?!\\/)或至少($|[^\\/]) Right now, it'll match any number of non-slashes — including zero. 现在,它将匹配任意数量的非斜杠-包括零。

我尝试过:在Mac的RegExr中工作的^(word1?\\/.+?\\/word2($|[^\\/]*)) ,我输入了: word/2342/word2/word/2342/word2/和它仅与第一个word/2342/word2匹配,但这只会检查字符串是否以您要求的开头...即使是简单的(word)\\/[0-9]{4}\\/word2也可以使用并允许中间有4个数字..但是您的问题让您感到困惑,您真正想要的是什么?

(.+\\bword2\\b)(?!\\/)呢?

In regex /^abc$/ , the ^ is to find pattern only in the beginning of the string and the $ is to find pattern only at the end of the string. 在正则表达式/^abc$/^仅在字符串的开头查找模式,而$仅在字符串的末尾查找模式。 Combined it will try to find a match with the whole string, it is like all or nothing match. 结合起来它将尝试找到整个字符串的匹配项,就像完全匹配或不匹配一样。

So put your regex to match in the string in the place of abc . 因此,将您的正则表达式放在字符串中,以匹配abc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM