fork(2) download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.util.regex.*;
  5. import java.lang.*;
  6. import java.io.*;
  7.  
  8. /* Name of the class has to be "Main" only if the class is public. */
  9. class Ideone
  10. {
  11. public static void main (String[] args) throws java.lang.Exception
  12. {
  13. try {
  14. String script = "IF N = 'abc' OR N = 'def' OR N = 'ghi' THEN";
  15.  
  16. // Groups:
  17. // 1 = comparison value
  18. // 2 = "or" list
  19. // 1 = "or" comparison value
  20. Pattern pattern = Pattern.compile("IF N = '([^']*)'(( OR N = '([^']*)')*) THEN");
  21. Matcher matcher = pattern.matcher(script);
  22. System.out.println("matches: "+ matcher.matches());
  23. System.out.println("if "+matcher.group(1));
  24.  
  25. // just the "OR ..." parts
  26. String orList = matcher.group(2);
  27. Pattern innerPattern = Pattern.compile(" OR N = '([^']*)'");
  28. Matcher innerMatcher = innerPattern.matcher(orList);
  29. while(innerMatcher.find()){
  30. System.out.println(" or "+innerMatcher.group(1));
  31. }
  32. }
  33. catch(Exception e){
  34. e.printStackTrace(System.out);
  35. }
  36. }
  37. }
Success #stdin #stdout 0.1s 320320KB
stdin
Standard input is empty
stdout
matches: true
if abc
  or def
  or ghi