正則表達式提取雙引號中的字符串


正則表達式提取雙引號中的字符串

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

 

例:

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

  

得到的結果便是去掉雙引號之后的結果。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM