import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
String s = "字符串sfsdfsdf胜多负少的【年月日】是的郭德纲";
Pattern pattern = Pattern.compile("【([^】]+)】"); // 正则表达式
Matcher matcher = pattern.matcher(s);
while (matcher.find()) {
System.out.println(matcher.group(1)); // 输出匹配的结果
}
}
}
java-正则 在字符串中找到【】中间的字符
该代码片段展示了如何使用Java的正则表达式Pattern和Matcher类从给定字符串中提取符合特定模式的部分,即【】之间的内容。
摘要由CSDN通过智能技术生成