Skip to main content
 首页 » 编程设计

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

2026年04月21日53lvdongjie

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

我认为该语言是 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 .