需求
在開發過程需要獲取字符串中在小括號內的內容,遂記錄一下
實現
import cn.hutool.core.util.ReUtil;
public class RegexTest {
public static void main(String[] args) {
String str = "username(zhangsan)";
String regex = "\\((.*?)\\)";
String group0 = ReUtil.getGroup0(regex, str);
String group1 = ReUtil.getGroup1(regex, str);
System.out.println("group0 = " + group0);
System.out.println("group1 = " + group1);
}
}
輸出結果
group0 = (zhangsan)
group1 = zhangsan