2015-03-31 30 views
0

我想寫一個正則表達式發生在標籤內的多個空格或字符。我寫了這樣的正則表達式 -正則表達式的Java - 檢查空間或字符

[tag:title](.*?)[tag:/title]. 

但是在字符串中如果有多個空格後它不匹配它。如果有一個空格或字符與它匹配。 我也試過

<title>([\\s.]*?)</title> 

但它不起作用。 請幫我糾正我的正則表達式。

我的計劃是 -

 public static void main(String[] args) { 
     String source1; 
     source1="<tag>    apple</tag>   <b>hello</b>  <tag>  orange gsdggg </tag> <tag>p wfwfw ear</tag>"; 

     System.out.println(Arrays.toString(getTagValues(source).toArray())); 
} 


private static List<String> getTagValues(String str) { 

     if (str.toString().indexOf("&amp;") != -1) 
      { 
      str = str.toString().replaceAll("&amp;", "&");// replace &amp; by & 
      // System.out.println("removed &amp formatted--" + source); 
      } 


     final Pattern TAG_REGEX = Pattern.compile("<title>(.*?)</title>"); 
     final List<String> tagValues = new ArrayList<String>(); 
     final Matcher matcher = TAG_REGEX.matcher(str); 
     int count=0; 
     while (matcher.find()) { 
      tagValues.add(matcher.group(1)); 
      count++; 
     } 
     System.out.println("Total occurance is -" + count); 
     return tagValues; 

}

+0

您介意發佈您正在處理的文本的樣本嗎? – 2015-03-31 16:21:31

+0

基本上,你想要'''標籤內的一切,呵呵? – 2015-03-31 16:27:32

+0

您可以使用'(\\ s |。)*' – hwnd 2015-03-31 16:32:00

回答

1

如果要檢查您必須使用多個空格:

\\s+ 

所以你的模式將變爲:

([\\s+.]*/)] 

或者你可以使用som如:

(\\s|.)* 

閱讀關於正則表達式here

您可以閱讀字符串模式here的教程。

+0

我試過了。沒有一個工作。不過謝謝。 – 2015-03-31 17:32:20

相關問題