fork download
  1. import java.util.ArrayList;
  2. import java.util.List;
  3. import java.util.regex.MatchResult;
  4. import java.util.regex.Matcher;
  5. import java.util.regex.Pattern;
  6. import java.util.stream.Collectors;
  7.  
  8. public class Main {
  9. public static void main(String[] args) {
  10. String str = "this is a test of proper nouns @Ryan";
  11.  
  12. List<String> list = Pattern.compile("@?\\w+|\\b(?!$)")
  13. .matcher(str)
  14. .results()
  15. .map(MatchResult::group)
  16. .collect(Collectors.toList());
  17.  
  18. System.out.println(list);
  19.  
  20. // #############Non-Stream Solution#############
  21. Matcher matcher = Pattern.compile("@?\\w+|\\b(?!$)").matcher(str);
  22. List<String> parts = new ArrayList<>();
  23. while (matcher.find()) {
  24. parts.add(matcher.group());
  25. }
  26.  
  27. System.out.println(parts);
  28. }
  29. }
Success #stdin #stdout 0.08s 49260KB
stdin
Standard input is empty
stdout
[this, , is, , a, , test, , of, , proper, , nouns, , @Ryan]
[this, , is, , a, , test, , of, , proper, , nouns, , @Ryan]