只在前面没有另一个字符串时匹配字符串

时间:2022-08-05 22:06:21

I want to match strings containing MYSTUFF in all cases, except when MYSTUFF is preceded by ENABLED_. So that the following would match:

我希望在所有情况下都匹配包含MYSTUFF的字符串,除非MYSTUFF前面有ENABLED_。因此,以下内容将匹配:

  • MYSTUFF
  • MYSTUFF
  • m_MYSTUFF
  • m_MYSTUFF
  • MYSTUFFIsGreat
  • MYSTUFFIsGreat
  • allOf_MYSTUFF
  • allOf_MYSTUFF

but the following wouldn't:

但以下不会:

  • ENABLED_MYSTUFF
  • ENABLED_MYSTUFF
  • m_ENABLED_MYSTUFF
  • m_ENABLED_MYSTUFF
  • ENABLED_MYSTUFFIsGreat
  • ENABLED_MYSTUFFIsGreat
  • allOf_ENABLED_MYSTUFF
  • allOf_ENABLED_MYSTUFF

I tried several variations using negative lookahead (variations of \w*(?!.*ENABLED_)MYSTUFF\w*), and conditional (variations of (?(?!=ENABLED_)(MYSTUFF))), but I did not manage to get the results I'm after.

我尝试了几种不同的方法,使用消极的前视(w*(?!.*ENABLED_)MYSTUFF\w*)和有条件的(?(?)=ENABLED_ (MYSTUFF))),但是我没有得到我想要的结果。

Is what I want even doable with regexes?

我想要的是regexes吗?

1 个解决方案

#1


2  

You could accomplish this by using a negative look-behind assertion ...

你可以通过消极的背后判断来实现这一点。

\w*(?<!ENABLED_)MYSTUFF\w*

see regex demo

查看演示正则表达式

#1


2  

You could accomplish this by using a negative look-behind assertion ...

你可以通过消极的背后判断来实现这一点。

\w*(?<!ENABLED_)MYSTUFF\w*

see regex demo

查看演示正则表达式