菜鸟笔记
提升您的技术认知

用正则表达式匹配双引号中的内容

匹配表达式:

\"([^\"]*)\"

匹配结果实验:

		String t = "\"world\"";
		String p = "\"([^\"]*)\"" ;
		Pattern P=Pattern.compile(p);
	      Matcher matcher1=P.matcher(t); 
	      if(matcher1.find())
	      {
	    	   System.out.println(matcher1.group(0));
	      }

代码中通过调用group()函数来得到匹配到的结果,如下:

"world"

但是我们想要双引号中的内容,可以对group()函数得到的结果进行一下处理,如下:

		String t = "\"world\"";
		String p = "\"([^\"]*)\"" ;
		Pattern P=Pattern.compile(p);
	    Matcher matcher1=P.matcher(t); 
	    if(matcher1.find())
	    {
	    	System.out.println(matcher1.group(0).replaceAll(p, "$1"));
	    }

得到的结果便是去掉双引号之后的结果。