简体   繁体   中英

Regex|Ignore second word(s) of string if it contains certain words

/((?:(?:is|will) (?!is))[^<?]+)/i

Test sentences:

  • text can be infront, Will is a good guy?
  • same here. Will you be able to help me?

I dont want it to match the first sentence and I want it to match the second.(which it does atm)

I am trying to learn how I can return the whole regex as false if the word after "is|will" is "is", but it keeps matching and eventually finds a match in example number one. I am kind of new to regex, so all help is appreciated.

This is what the match looks like:

比赛看起来像什么

Thanks in advance

Your expression seems fine, but you need to anchor it so that it starts matching from the beginning of the text. You can use ^ at the start of the regex.

/^((?:(?:is|will) (?!is))[^<?]+)/i

Without the anchor, the regex engine tries to find a match anywhere in the string. In the case of the first sentence, it can match the word is as the first word in the expression, and then the rest of the expression matches.

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