思路
利用Matcher对象的find()方法与while循环结合,匹配到字符串中所有与正则匹配的子串。在循环体中利用Matcher对象的group()方法拿到当前匹配到的子字符串。
示例代码
@Test
public void testRegexp() {
String s = "Hi Job,(213,456) and (AAA,/* notes */BBB) ,()oooo(abc,bcd,efg);";
if (s != null && !"".equals(s)) {
String regex = "\\([\\w\\/\\*\\s,]*\\)";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(s);
while (matcher.find()) {
String matcherString = matcher.group();
System.out.println(matcherString);
}
}
}
本文介绍了一种利用Java中的Matcher对象与正则表达式结合的方法来查找并匹配字符串中的特定括号内容。通过示例代码展示了如何提取包含特定字符的括号内的子字符串。

2184

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



