我正在寻找一个正则表达式来匹配花括号中的文本
{one}{two}{three}
我希望每一组都是独立的,就像onetwothree
分开来。
我试过Pattern.compile("\\{.*?\\}");
,但只去掉前面和最后的花括号,有什么好的解决方法呢?
发布于 2018-08-13 14:12:04
这样试试:
String s = "{one}{two}{three}";
Pattern p = Pattern.compile("\\{([^}]*)\\}");
Matcher m = p.matcher(s);
while (m.find()) {
System.out.println(m.group(1));
}
输出
one
two
three
如果想要三个特定的比赛组
String s = "{one}{two}{three}";
Pattern p = Pattern.compile("\\{([^}]*)\\}\\{([^}]*)\\}\\{([^}]*)\\}");
Matcher m = p.matcher(s);
while (m.find()) {
System.out.println(m.group(1) + ", " + m.group(2) + ", " + m.group(3));
}
输出
one, two, three
发布于 2018-08-13 14:27:06
如果想要3个组,你的模式需要3个组
"\\{([^}]*)\\}\\{([^}]*)\\}\\{([^}]*)\\}"
^^^^^^^^^^^^^
https://stackoverflow.com/questions/-100002103
复制相似问题