正则表达式查找 XXX(如果存在)但如果 Y 也存在则不查找

Regex find XXX if present but not if Y is present too

我想要一个正则表达式来查找每个包含“好答案”的字符串,但如果字符串还包含问号(“?”)则不查找。

我认为语言是 Ruby(它用于 interpersonal.stackexchange.com 上的 ips comment bot)。

到目前为止,我的正则表达式看起来像这样:

((?!\?)(.*?))good\Wanswer((.*?)(?!\?))$

根本不起作用...

我也有这个版本可以处理我想要的一些字符串(但检测到太多东西):

good\Wanswer(((?!\?).)*$)

这里是我要检测的字符串:

That a very good answer!

_

(Other than that, this is a really good answer and I've upvoted it.)

_

good answer dvdf!

_

dsds good answer cfds

_

It is a good answer, but I feel it played into the OP's hands really. You said what they wanted to hear - that they shouldn't freely share the information because it is like a child cheating on their homework. It is contrary to the spirit of this site, and I'm not sure that charging a colleague money to learn something work-based from you won't get the OP into trouble with their employer. Imagine if a doctor asked to confer with a fellow doctor in order to help a patient, and they charged each other for the information they shared. They'd be dismissed.


这是我不想检测的

Thanks for this good answer! (I upvoted it) However, I still don't understand why I shouldn't mention that I believe whatever? What's the problem with that...

_

cxvd good answer? zedfs

_

ezdds? good answer dsf dsf

_

sdsd? dsfdsf? good answer!

_

Hi, the question is "How to tell blabla when X is my good friend", would you mind be a little more detail about how OP should do that when OP still want to be friend with both parties? Also, please take some time to read "How do I write a good answer

_

As a side note, here is a link to "How do I write a good answer?

_

Hi, this sound like a good answer to me, I just have one question though: Where you in a similar situation before where you successfully used this technic? In here it's better to back-up your answer with personal experience (here is a guide to How to write a good answer if you need it)

此外,如果你想让它不区分大小写,请成为我的客人

您的机器人 运行正在 Ruby 上运行,您可以使用像

这样的正则表达式
(?i)^(?![^?]*\?).*good\s+answer

或者,如果您 运行 针对多行输入的正则表达式并且不想跨行溢出

(?i)^(?![^?\n]*\?).*good +answer

参见the regex demo

正则表达式详细信息

  • (?i) - 不区分大小写的修饰符
  • ^ - 行首
  • (?![^?]*\?) - 除了 ? 之外的任何 0+ 个字符之后不允许 ?
  • .* - 任意 0+ 个字符,尽可能多
  • good\s+answer - good, 1+ 个空格, answer.