fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6. import java.util.regex.Matcher;
  7. import java.util.regex.Pattern;
  8.  
  9. /* Name of the class has to be "Main" only if the class is public. */
  10. class Ideone
  11. {
  12. public static void main (String[] args) throws java.lang.Exception
  13. {
  14.  
  15.  
  16.  
  17. final String regex = "update\\s+(\\w+)\\s+|(\\S+\\s*=\\s*\\S+)(?=.*where)|(where.*)";
  18. final String string = "UPDATE TEMP SET COL1 = -1 , COL2 = 'val' Where COL3 > 45\n";
  19.  
  20. final Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
  21. final Matcher matcher = pattern.matcher(string);
  22.  
  23. while (matcher.find()) {
  24. for (int i = 1; i <= matcher.groupCount(); i++) {
  25. if(matcher.group(i)!=null)
  26. System.out.println("Group " + i + ": " + matcher.group(i));
  27. }
  28. }
  29.  
  30. }
  31. }
Success #stdin #stdout 0.1s 27944KB
stdin
Standard input is empty
stdout
Group 1: TEMP
Group 2: COL1 = -1
Group 2: COL2 = 'val'
Group 3: Where COL3 > 45