我是正则表达式新手。我如何从下面的行中分割点数据:
((X1,Y1),(X2,Y2),(X3,Y3))
拆分为:
(X1,Y1)
(X2,Y2)
(X3,Y3)
提前致谢:)
最佳答案
嗯,当引入嵌套括号时,使用正则表达式从括号或圆括号中提取内容可能很快就会变得复杂。但仍然在您当前的情况下,似乎您可以使用 Pattern
获得结果和 Matcher
类(不要尝试 split
,因为它会稍微复杂一些):
String str = "((X1,Y1),(X2,Y2),(X3,Y3))";
// The below pattern will fail with nested brackets - (X1, (X2, Y2)).
// But again, that doesn't seem to be the case here.
Matcher matcher = Pattern.compile("[(][^()]*[)]").matcher(str);
while (matcher.find()) {
System.out.println(matcher.group());
}
关于java - 要分割的正则表达式 (...,...),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14920545/