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 TestReg
  11. {
  12. public static void main (String[] args) throws java.lang.Exception
  13. {
  14. String[] samples = {"outside \"inside\" outside",
  15. "outside \"ins\\\"ide\" outside",
  16. "outside \"\\\\\"",
  17. "Hello \"my\" name is \"Andy\", nice to meet you.",
  18. "Foo \"bar\" baz\\\"\\\\\"qux\".",
  19. "\"\\\"\" \"\\\"\" \\\\\"s\\\"s\\\\\"g \\\"x\"x\"\n", "\"ddd\" \"asdasd\" dfdsf \"asdasd\\\"\"" +
  20. " \"\\\\\" \"something\\g\" \"\" Hello \"my\" name is \"Andy\", nice to meet you. Foo \"bar\" " +
  21. "baz\\\"\\\\\"qux\".\"\\\"\" \"\\\"\" \\\\\"s\\\"s\\\\\"g \\\"x\"x\""};
  22. TestReg testReg = new TestReg();
  23. for(String sample : samples) {
  24. System.out.println(testReg.testIt(sample));
  25. }
  26. }
  27.  
  28. public String testIt(String sample){
  29. Matcher matcher = Pattern.compile("(?<!.\\G\")(?<!(?:^|[^\\\\])(?:\\\\\\\\){0,20}\\\\\")(?:(?<=\")(?:(?:\\\\\\\\|\\\\\"|[^\"])+?)(?=\")|(?<=\")(?=\"))").
  30. matcher(sample);
  31.  
  32. String result = "";
  33. while(matcher.find()){
  34. result += matcher.group() +", ";
  35. }
  36.  
  37. return result;
  38. }
  39.  
  40.  
  41.  
  42. }
Success #stdin #stdout 0.12s 320512KB
stdin
Standard input is empty
stdout
inside, 
ins\"ide, 
\\, 
my, Andy, 
bar, qux, 
\", \", s\"s\\, x, 
ddd, asdasd, asdasd\", \\, something\g, , my, Andy, bar, qux, \", \", s\"s\\, x,