fork download
  1. import java.util.*;
  2. import java.util.regex.*;
  3. import java.lang.*;
  4. import java.io.*;
  5.  
  6. class Ideone
  7. {
  8. public static void main (String[] args) throws java.lang.Exception
  9. {
  10. String regex = "(?U)\\b(?!(?:word1|word2)\\b)\\w+";
  11. Pattern p = Pattern.compile(regex);
  12. Matcher m = p.matcher("Extract all words but word1 and word2.");
  13. List<String> res = new ArrayList<>();
  14. while(m.find()) {
  15. res.add(m.group());
  16. }
  17. System.out.println(res);
  18. }
  19. }
Success #stdin #stdout 0.07s 33772KB
stdin
Standard input is empty
stdout
[Extract, all, words, but, and]