如何在Java中终止正则表达式匹配



给定一个字符串:

hello"this is a test"this is another test"etc

如何编写一个正则表达式,选择"之前的任何内容,然后继续进行下一个匹配?所以最后,我得到了以下匹配:

hello
this is a test
this is another test
etc

使用字符串方法"split"with"作为分隔符。

http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/String.html#split(java.lang.String)

编辑:

String s = "hello"this is a test"this is another test"etc";
String matches[] = s.split(""");
for (String str : matches) System.out.println(str);

给出

hello
this is a test
this is another test
etc

您可能正在寻找类似[^"]+ 的东西

重复一次或多次,任何不是" 的字符

示例:

String s = "hello"this is a test"this is another test"etc";
Matcher matcher = Pattern.compile("[^"]+").matcher(s);
while (matcher.find()) {
    System.out.println(s.substring(matcher.start(),matcher.end()));
}

将产生:

hello
this is a test
this is another test
etc

最新更新