是否有括号(中英文括号)
public class CheckBracket {
public static void main(String[] args) {
String[] testStrings = {"这是包含中文括号()的字符串", "This is a string with English brackets()", "这是没有括号的字符串", "This has no brackets"};
for (String str : testStrings) {
if (containsBracket(str)) {
System.out.println(str + " 包含中文或英文括号");
} else {
System.out.println(str + " 不包含中文和英文括号");
}
}
}
public static boolean containsBracket(String str) {
// 正则表达式匹配中文括号或英文括号
return str.matches(".*[\\<.*?\\>|\\(.*?\\)|\\(.*?\\)|\\(.*?\\)|\\(.*?\\)|\\[.*?\\]|\\【.*?\\】|\\{.*?\\}].*");
}
}
获取括号内的内容
public static void main(String[] args) {
String input = "(中文括号)、(英文括号)、(英中文括号)、(中英文括号)";
Pattern pattern = Pattern.compile("[((\\[][\\u4e00-\\u9fa5a-zA-Z0-9]+[))\\]]");
Matcher matcher = pattern.matcher(input);
while (matcher.find()) {
String content = matcher.group();
System.out.println(content.substring(1, content.length() - 1));
}
}
去除括号及里面的内容
public static void main(String[] args) {
String REGEX_ALL_BRACKETS = "\\<.*?\\>|\\(.*?\\)|\\(.*?\\)|\\(.*?\\)|\\(.*?\\)|\\[.*?\\]|\\【.*?\\】|\\{.*?\\}";
String context="这是测试的数据(test)";
String regexString = context.replaceAll(REGEX_ALL_BRACKETS, "");
System.out.println("regexString = " + regexString);
//输出结果:这是测试的数据
}