开发者

Regex ignore everything between 2 words

开发者 https://www.devze.com 2022-12-08 16:16 出处:网络
I have some strings to check against a Regex pattern. Here is the regex: apple(?!( ?not)).*?inc(\\.|luded)?

I have some strings to check against a Regex pattern. Here is the regex:

apple(?!( ?not)).*?inc(\.|luded)?

string 1: "banana, rotten apple not included" - does not give me a match which is what I want.

string 2: "banana, rotten apple included" - produces a match which is what I want

But if I have a string like...

"banana, rotten apple, ripe avocado not included" or "banana, rotten apple and apricot not included" both produces a match which I don't want.

So basically, I开发者_开发技巧 want to see if both "apple" and "included" (or "inc" or "inc.") are in the string and NO "not" before "included" and ignore everything else in between.

Thanks!


How about:

apple.*(?<!\snot)\s+inc(\.|luded)


You're using a negative lookahead assertion that verifies that "not" does not appear right after "apple". But, if I understand you correctly, what you want is to check that "inc" is not immediately preceded by "not". You can use a negative lookbehind assertion, like the following:

apple.*(?<!(\bnot ))inc(\.|luded)?
0

精彩评论

暂无评论...
验证码 换一张
取 消