简体   繁体   中英

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. as soon as word2 is followed by a slash, do not match AT ALL

Nonetheless, it would still match the 'word/2342/word2' part of the second regex... I want it to not match anything at all

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. 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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