/* package whatever; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; import java.util.regex.Matcher; import java.util.regex.Pattern; /* Name of the class has to be "Main" only if the class is public. */ class TestReg { { "outside \"ins\\\"ide\" outside", "outside \"\\\\\"", "Hello \"my\" name is \"Andy\", nice to meet you.", "Foo \"bar\" baz\\\"\\\\\"qux\".", "\"\\\"\" \"\\\"\" \\\\\"s\\\"s\\\\\"g \\\"x\"x\"\n", "\"ddd\" \"asdasd\" dfdsf \"asdasd\\\"\"" + " \"\\\\\" \"something\\g\" \"\" Hello \"my\" name is \"Andy\", nice to meet you. Foo \"bar\" " + "baz\\\"\\\\\"qux\".\"\\\"\" \"\\\"\" \\\\\"s\\\"s\\\\\"g \\\"x\"x\""}; TestReg testReg = new TestReg(); } } Matcher matcher = Pattern.compile("(?<!.\\G\")(?<!(?:^|[^\\\\])(?:\\\\\\\\){0,20}\\\\\")(?:(?<=\")(?:(?:\\\\\\\\|\\\\\"|[^\"])+?)(?=\")|(?<=\")(?=\"))"). matcher(sample); while(matcher.find()){ result += matcher.group() +", "; } return result; } }
Standard input is empty
inside, ins\"ide, \\, my, Andy, bar, qux, \", \", s\"s\\, x, ddd, asdasd, asdasd\", \\, something\g, , my, Andy, bar, qux, \", \", s\"s\\, x,