fork(3) download
  1. import java.util.regex.Matcher;
  2. import java.util.regex.Pattern;
  3.  
  4. public class Main {
  5. public static void main(String args[]) throws Exception {
  6. String candidate = "I \n"+
  7. "like \n"+
  8. "to "+
  9. "eat "+
  10. "but "+
  11. "I "+
  12. "don't "+
  13. "like "+
  14. "to "+
  15. "eat "+
  16. "everyone's "+
  17. "food "+
  18. "'' '''' '.' ' "+
  19. "or "+
  20. "they'll "+
  21. "starv'e'";
  22.  
  23. String regex = "('\\w+)|(\\w+'\\w+)|(\\w+')|(\\w+)";
  24. Matcher matcher = Pattern.compile(regex).matcher(candidate);
  25. while (matcher.find()) {
  26. System.out.println("> matched: `" + matcher.group() + "`");
  27. }
  28. }
  29. }
Success #stdin #stdout 0.07s 215552KB
stdin
Standard input is empty
stdout
> matched: `I`
> matched: `like`
> matched: `to`
> matched: `eat`
> matched: `but`
> matched: `I`
> matched: `don't`
> matched: `like`
> matched: `to`
> matched: `eat`
> matched: `everyone's`
> matched: `food`
> matched: `or`
> matched: `they'll`
> matched: `starv'e`