[英]java split regex: split string using any text between curly brackets and keep the delimiter
[英]Java Regex to get Data between curly brackets
我正在寻找一个正则表达式来匹配大括号之间的文本。
{one}{two}{three}
我想每一个这些作为单独的群体, one
two
three
分开。
我试过Pattern.compile("\\\\{.*?\\\\}");
它只删除第一个和最后一个大括号。
您需要在要捕获的内容周围使用捕获组( )
。
只匹配和捕获大括号之间的内容。
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
如果你想要 3 组,你的模式需要 3 组。
"\\{([^}]*)\\}\\{([^}]*)\\}\\{([^}]*)\\}"
^^^^^^^^^^^^^
(中间部分与左右相同)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.