fork download
  1. import java.util.regex.Matcher;
  2. import java.util.regex.Pattern;
  3.  
  4. public class Main
  5. {
  6. static void printMatch(String regex, String str, int... groups)
  7. {
  8. System.out.println("String = " + str);
  9. System.out.println("Regex = " + regex);
  10. Pattern p = Pattern.compile(regex);
  11. Matcher m = p.matcher(str);
  12. while (m.find())
  13. for (int i: groups)
  14. System.out.println("Group " + i + " = " + m.group(i));
  15. System.out.println();
  16. }
  17.  
  18. public static void main(String[] args)
  19. {
  20. String regex = "\".+((?<=\")[^\"]*(?=\")|-).+\"";
  21. printMatch(regex, "\"this is a string \"xyz\" \"", 1);
  22. printMatch(regex, "\"this is a string - \"", 1);
  23. }
  24. }
Success #stdin #stdout 0.07s 380224KB
stdin
Standard input is empty
stdout
String = "this is a string "xyz" "
Regex = ".+((?<=")[^"]*(?=")|-).+"
Group 1 = xyz

String = "this is a string - "
Regex = ".+((?<=")[^"]*(?=")|-).+"
Group 1 = -