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("&") != -1)
{
str = str.toString().replaceAll("&", "&");// replace & by &
// System.out.println("removed & 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;
}
您介意發佈您正在處理的文本的樣本嗎? – 2015-03-31 16:21:31
基本上,你想要''
您可以使用'(\\ s |。)*' – hwnd 2015-03-31 16:32:00