使用 Pattern
和 Matcher
匹配字符串中 ()
之间的内容。示例代码如下:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
System.out.println("==================== start ====================");
String ua = "Mozilla/5.0 (Linux; Android 11; M2102K1C Build/RKQ1.201112.002; wv) " +
"AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/90.0.4430.210 " +
"Mobile Safari/537.36 hap/1.10/xiaomi org.hapjs.mockup/1.10.0.0 " +
"me.liujiajia.app.sample/1.3.1 ({\"packageName\":\"org.hapjs.mockup\"," +
"\"type\":\"other\",\"extra\":{}})";
Matcher matcher = Pattern.compile("(?<=\\()[^\\)]+").matcher(ua);
while (matcher.find()) {
String group = matcher.group();
System.out.println(group);
}
System.out.println("===================== end =====================");