简体   繁体   English

正则表达式不包含某些单词

[英]Regex does not contain certain words

I am looking for a regex expression that will exclude the below words from a huge text file/files. 我正在寻找一个正则表达式,该表达式将从巨大的文本文件中排除以下单词。

@author
@Autowired
@Override
@param
@SuppressWarnings

I have tried with this but does not work as expected. 我已经尝试过了,但是没有按预期工作。

@[^(author)(Autowired)(Override)(param)(SuppressWarnings)].*

Try using the following regex (using negative look-ahead ) : 尝试使用以下正则表达式(使用负数预

@(?!author|Autowired|Override|param|SuppressWarnings).*

see regex demo / explanation 参见正则表达式演示/说明

You can use a negative lookahead: 您可以使用否定的前瞻:

@(?!author|Autowired|Override|param|SuppressWarnings)\S+

Basically, it looks for a @ that is not followed by that list of words, and then it matches any non-whitespace characters after that. 基本上,它会寻找一个@ ,后面没有该单词列表,然后与之匹配的所有非空白字符。

Square brackets in regexes are used for character classes. 正则表达式中的方括号用于字符类。 When you put a list of characters in square brackets, this matches one character that is one of the ones listed. 当您将一个字符列表放在方括号中时,它将匹配一个字符 ,该字符是列出的字符之一。 So 所以

[author]

matches one character, if it's a , h , o , r , t , or u . 匹配一个字符(如果是ahortu It does not look for the word author . 它不查找author一词。 Putting ^ in front also looks for one character that isn't in the list: ^放在前面也会查找不在列表中的一个字符:

[^author]

matches one character as long as it's not a , h , o , r , t , or u . 匹配一个字符,只要它不是ahortu

But the key thing here is that [] cannot be used to match words or other sequences. 但是这里的关键是[]不能用于匹配单词或其他序列。 In your example, 在您的示例中

@[^(author)(Autowired)(Override)(param)(SuppressWarnings)].*

the part in square brackets matches one character that is not ( , a , u , or any of the other characters that appear in the square brackets (many of those characters appear multiple times, but that doesn't affect anything). 方括号中的部分与一个不是(au或其他任何出现在方括号中的字符)匹配(这些字符中的许多字符出现多次,但这并不影响任何字符)。

To flip the script, if you're actually trying to take the text file and remove things that are in your list of keywords, you'll probably want to find those using syntax more like this: @(author|AutoWired|Override|param|SuppressWarnings)\\b . 要翻转脚本,如果您实际上是在尝试获取文本文件并删除关键字列表中的内容,则可能需要使用以下语法查找它们: @(author|AutoWired|Override|param|SuppressWarnings)\\b The terminal \\b is just a precaution to avoid @authority or other unlikelihoods. 终端\\b只是为了避免@authority或其他不太可能的预防措施。

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

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