更新时间:2023-02-26 11:16:49
您应该能够使用非贪婪量词,特别是 *?.您可能需要以下内容:
You should be able to use non-greedy quantifiers, specifically *?. You're going to probably want the following:
Pattern MY_PATTERN = Pattern.compile("\[(.*?)\]");
这将为您提供一个模式,该模式将匹配您的字符串并将文本放在第一组的方括号内.查看模式API文档 了解更多信息.
This will give you a pattern that will match your string and put the text within the square brackets in the first group. Have a look at the Pattern API Documentation for more information.
要提取字符串,您可以使用以下内容:
To extract the string, you could use something like the following:
Matcher m = MY_PATTERN.matcher("FOO[BAR]");
while (m.find()) {
String s = m.group(1);
// s now contains "BAR"
}