正则表达式:匹配不包含的单词 [英] Regex: Match word not containing

查看:82
本文介绍了正则表达式:匹配不包含的单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的话:

EFI 内部外壳电喷硬盘电喷驱动

我想匹配包含 EFI 但不包含 Drive 的单词.所以只有最上面的词 (EFI Internal Shell) 应该匹配.

如何在正则表达式中做到这一点?

我浏览了 SO,没有一个答案能让我走上正轨.

例如:不包含特定字符串的正则表达式 说要使用 ^((?!my string).)*$ 但是没用.甚至匹配任何不包含 Drive 的字符串.

有什么建议吗?

解决方案

您的 ^((?!Drive).)*$ 根本不起作用,因为您针对多行输入进行了测试.

您应该使用 /m 修饰符来查看 正则表达式匹配的内容.它只匹配不包含 Drive 的行,但是 tempered greedy token 不检查 EFI 是否在字符串内.

实际上,$ 锚在这里是多余的,因为 .* 匹配除换行符以外的任何零个或多个字符.您可以简单地将其从您的模式中删除.

(注意:在 .NET 中,您需要使用 [^\r\n]* 而不是 .* 因为 <.NET 模式中的 code>. 匹配除换行符、LF、char 之外的任何字符,并匹配所有其他换行符字符,如回车、CR 等).

使用类似的东西

^(?!.*Drive).*EFI.*

或者,如果您只需要在 Drive全字形式存在的情况下才使匹配失败:

^(?!.*\bDrive\b).*EFI.*

或者,如果您想用更多词来表示失败:

^(?!.*(?:Drive|SomethingElse)).*EFI.*^(?!.*\b(?:Drive|SomethingElse)\b).*EFI.*

参见正则表达式演示

这里,

  • ^ - 匹配字符串的开头
  • (?!.*Drive) - 确保字符串中没有Drive"(因此,不允许使用 Drives)
  • (?!.*\bDrive\b) - 确保字符串中没有Drive"作为整个词(因此,Drives 是允许的)
  • .* - 除换行符以外的任何 0+ 个字符,尽可能多
  • EFI - EFI 子串
  • .* - 除换行符以外的任何 0+ 个字符,尽可能多.

如果您的字符串有换行符,请使用 /s dotall 修饰符或将 . 替换为 [\s\S].>

I have the following words:

EFI Internal Shell
EFI Hard Drive
EFI Drive

I want to match words that contain EFI but not containing Drive. So only the top word (EFI Internal Shell) should match.

How can this be done in regex?

I looked through SO and none of the answers were able to get me on the right track.

For example: Regular expression that doesn't contain certain string says to use ^((?!my string).)*$ but that didn't work. Even to match any string not containing Drive.

Any tips?

解决方案

Your ^((?!Drive).)*$ did not work at all because you tested against a multiline input.

You should use /m modifier to see what the regex matches. It just matches lines that do not contain Drive, but that tempered greedy token does not check if EFI is inside the string.

Actually, the $ anchor is redundant here since .* matches any zero or more characters other than line break characters. You may simply remove it from your pattern.

(NOTE: In .NET, you will need to use [^\r\n]* instead of .* since . in a .NET pattern matches any char but a newline, LF, char, and matches all other line break chars, like a carriage return, CR, etc.).

Use something like

^(?!.*Drive).*EFI.*

Or, if you need to only fail the match if a Drive is present as a whole word:

^(?!.*\bDrive\b).*EFI.*

Or, if there are more words you want to signal the failure with:

^(?!.*(?:Drive|SomethingElse)).*EFI.*
^(?!.*\b(?:Drive|SomethingElse)\b).*EFI.*

See regex demo

Here,

  • ^ - matches start of string
  • (?!.*Drive) - makes sure there is no "Drive" in the string (so, Drives are NOT allowed)
  • (?!.*\bDrive\b) - makes sure there is no "Drive" as a whole word in the string (so, Drives are allowed)
  • .* - any 0+ chars other than line break chars, as many as possible
  • EFI - anEFI substring
  • .* - any 0+ chars other than line break chars, as many as possible.

If your string has newlines, either use a /s dotall modifier or replace . with [\s\S].

这篇关于正则表达式:匹配不包含的单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆