简体   繁体   中英

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 . It does not look for the word 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 .

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).

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 . The terminal \\b is just a precaution to avoid @authority or other unlikelihoods.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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