利用正则表达式获取字符串中想要的值:
获取符串场景: 你好呀(嘻嘻)
我需要获取“嘻嘻” 该如何获取呢?
1.通过正则表达式获取:
String str = "你好呀(嘻嘻)";
Pattern p = Pattern.compile("\\(([^\\)]+)");
Matcher matcher = p.matcher(str);
if (matcher.find() && matcher.groupCount() >= 1){
System.out.println(matcher.group(1));
}
2.通过字符截取获取:
String str = "你好呀(嘻嘻)";
String xixi = str.substring(str.indexOf("(") + 1, str.indexOf(")"));
System.out.println(xixi);

本文介绍如何使用正则表达式从特定格式的字符串中精确提取括号内的内容,包括两种方法:一是利用正则表达式的匹配功能;二是采用字符串截取的方式。文章详细展示了代码实现过程,适合对正则表达式感兴趣或需要解决类似问题的读者。
1354

被折叠的 条评论
为什么被折叠?



