public static void main(String[] args) {
String text = "hi ravi \"how are you\" when are you coming";
String regex = "\"([^\"]*)\"|(\\S+)";
Matcher m = Pattern.compile(regex).matcher(text);
while (m.find()) {
if (m.group(1) != null) {
System.out.println("Quoted [" + m.group(1) + "]");
} else{
System.out.println("Plain [" + m.group(0) + "]");
}
}
// getSplits(text);
}输出:
素喜 拉维平原 引用你的话 朴素时 平原区 朴素的你 平地来
如果给定的文本只有一个单引号,那么上面的代码可以正常工作。有谁能帮助我用下面的输入获得低于输出的结果吗?
text = "hi ravi \"\"how are\" you\" when are you coming";预期产出:
素喜 拉维平原 引用“how ‘s” you 朴素时 平原区 朴素的你 平地来
发布于 2016-07-07 20:35:42
下面的regex适用于您的示例输入/输出。您必须对预期的结果提供更详细的描述,因为这可能不是您预期的结果。
public static void main(String[] args) {
String text = "hi ravi \"\"how are\" you\" when are you coming";
String regex = "(\".+\")|(\\S+)";
Matcher m = Pattern.compile(regex).matcher(text);
while (m.find()) {
if (m.group(1) != null) {
System.out.println("Quoted [" + m.group(1) + "]");
} else{
System.out.println("Plain [" + m.group(0) + "]");
}
}
// getSplits(text);
}发布于 2016-07-07 20:27:54
https://stackoverflow.com/questions/38253383
复制相似问题