fork download
  1. import java.util.*;
  2. import java.lang.*;
  3. import java.io.*;
  4. import java.util.regex.*;
  5.  
  6. class Ideone
  7. {
  8. public static void main (String[] args) throws java.lang.Exception
  9. {
  10. // String s = "this*string'has only two@tokens'"; // => [this, stringhas only two@tokens]
  11. String s = "one'two''three' '' four 'twenty-one'";
  12. Pattern pattern = Pattern.compile("(?:\\w|'[^']*')+", Pattern.UNICODE_CHARACTER_CLASS);
  13. Matcher matcher = pattern.matcher(s);
  14. List<String> tokens = new ArrayList<>();
  15. while (matcher.find()){
  16. tokens.add(matcher.group(0).replace("'", ""));
  17. }
  18. System.out.println(tokens);
  19. }
  20. }
Success #stdin #stdout 0.09s 34288KB
stdin
Standard input is empty
stdout
[onetwothree, , four, twenty-one]