我正在尝试编写一个正则表达式,以便在标记内出现多个空格或字符。我写了像这样的正则表达式 -
[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;
}