简体   繁体   English

如何使用正则表达式匹配不在另一个字符串中出现的字符串?

[英]How can I match occurrences of string not in another string using regular expressions?

I'm trying to match all occurances of "string" in something like the following sequence except those inside @@ 我正在尝试按照以下顺序匹配“字符串”的所有出现, @@内部的除外

as87dio u8u u7o @string@ ou os8 string os u

ie the second occurrence should be matched but not the first 即第二次出现应匹配,但第一次不匹配

Can anyone give me a solution? 谁能给我解决方案?

You can use negative lookahead and lookbehind : 您可以使用负数前瞻和后瞻

(?<!@)string(?!@)

EDIT 编辑

NOTE: As per Marks comments below, this would not match @string or string@ . 注意:根据下面的Marks注释,这与@stringstring@不匹配。

你可以试试:

(?:[^@])string(?:[^@])

OK, 好,

If you want to NOT match a character you put it in a character class (square brackets) and start it with the ^ character which negates it, for example [^a] means any character but a lowercase 'a'. 如果不想与某个字符匹配,可以将其放在字符类(方括号)中,并以^字符将其取反,例如[^a]表示除小写字母'a'以外的任何字符。

So if you want NOT at-sign, followed by string, followed by another NOT at-sign, you want 因此,如果您想要“非标志”,然后是字符串,然后是另一个“非标志”,则需要

[^@]string[^@]

Now, the problem is that the character classes will each match a character, so in your example we'd get " string " which includes the leading and trailing whitespace. 现在,问题在于字符类将各自匹配一个字符,因此在您的示例中,我们将获得“ string”,其中包括前导和尾随空格。 So, there's another construct that tells you not to match anything, and that is parens with a ?: in the beginning. 因此,还有另一种结构告诉您什么都不匹配,并且在开始时用?进行了限制。 (?: ) . (?: ) So you surround the ends with that. 因此,您将其围绕在两端。

(?:[^@])string(?:[^@])

OK, but now it doesn't match at the start of string (which, confusingly, is the ^ character doing double-duty outside a character class) or at the end of string $ . 可以,但是现在它在字符串的开头(令人困惑的是, ^字符在字符类之外执行双重任务)或在字符串$的末尾不匹配。 So we have to use the OR character | 所以我们必须使用OR字符| to say "give me a non-at-sign OR start of string" and at the end "give me an non-at-sign OR end of string" like this: 说“给我一个字符串的非符号或结尾”,最后说“给我一个字符串的非符号或结尾”,如下所示:

(?:[^@]|^)string(?:[^@]|$)

EDIT: The negative backward and forward lookahead is a simpler (and clever) solution, but not available to all regular expression engines. 编辑:负向后和前向超前是一种更简单(且更聪明)的解决方案,但不适用于所有正则表达式引擎。

Now a follow-up question. 现在是一个后续问题。 If you had the word "astringent" would you still want to match the "string" inside? 如果您有“涩味”一词,您是否仍要匹配其中的“字符串”? In other words, does "string" have to be a word by itself? 换句话说,“字符串”是否必须本身就是一个词? (Despite my initial reaction, this can get pretty complicated :) ) (尽管我最初的反应是,这可能变得相当复杂:))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM