@Test public void test() { Pattern pattern = Pattern.compile("\"(\\w+)\":\"(\\w+)\""); Matcher m = pattern.matcher("\"KEY\":\"VAL\""); m.group(1); }
请问这个正则为什么无法匹配. 脑子进猪油了,现在
欢迎选择我的课程,让我们一起见证您的进步~~
There is nothing wrong with your expression, you just need to adjust it firstm.find()再取m.group(1)
m.find()
m.group(1)
因为'\'和'"'(双引号)是需要要转义符的'\'的.像下面写就能行了. Pattern pattern = Pattern.compile("\\"(\w+)\\":\\"(\w+)\\""); Matcher m = pattern.matcher("\"KEY\":\"VAL\""); //m.group(1); if(m.find()){ System.out.println(m.group(1)); }
There is nothing wrong with your expression, you just need to adjust it first
m.find()
再取m.group(1)