更新时间:2023-02-26 11:16:19
你应该能够使用非贪婪量词,特别是* ?.您可能需要以下内容:
You should be able to use non-greedy quantifiers, specifically *?. You're going to probably want the following:
Pattern MY_PATTERN = Pattern.compile("\\[(.*?)\\]");
这将为您提供一个与您的字符串匹配的模式,并将文本放在方括号中第一组。查看 Pattern 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"
}