正则表达式提取双引号中的字符串

正则表达式提取双引号中的字符串

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

 

例:

<my:Control x:Name="aa" RowCount="{StaticResource RowCount}" ColumnCount="{StaticResource ColumnCount}" RowSpacing="{StaticResource RowSpacing}" >

匹配结果:

 

JAVA调用例:

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"));
}

  

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

 

posted @ 2022-03-23 08:41  奥兰王子  阅读(3206)  评论(0编辑  收藏  举报