Java Regex在大括号之间获取数据 [英] Java Regex to get Data between curly brackets
本文介绍了Java Regex在大括号之间获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在寻找一个正则表达式来匹配花括号之间的文本。
I am looking for a regular expression to match the text between curly brackets.
{one}{two}{three}
我希望每个人都是单独的组,因为一个
两个
三个
分开。
I want each of these as separate groups, as one
two
three
separately.
我试过 Pattern.compile(\\ {。 *?\\});
仅删除第一个和最后一个花括号
I tried Pattern.compile("\\{.*?\\}");
which removes only first and last curly brackets
谢谢。
推荐答案
您需要使用捕获组()
围绕您想捕获的内容。
You need to use a capturing group ( )
around what you want to capture.
仅匹配并捕捉大括号之间的内容。
To just match and capture what is between your curly brackets.
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
如果你想要三个特定的匹配组...
If you want three specific match groups...
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
这篇关于Java Regex在大括号之间获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文