繁体   English   中英

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

[英]How to terminate a regex match in Java?

给定一个字符串:

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

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

hello
this is a test
this is another test
etc

使用字符串方法“ split”和“作为分隔符。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM