I want to count all tags which has some text in it. I don't want to count tag which has only spaces and no text in it. For ex- in the program below also counts first tag which only has spaces in it and there is no value. So the output is- Total occurrences are -3 [ , orange gsdggg , p wfwfw ear]. Which is wrong since it should have given only 2 occurrences and -[orange gsdggg , p wfwfw ear].
Please help me to figure this out.
My program is -
public static void main(String[] args) {
String source1;
source1="<tag> </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("<tag>(.+?)</tag>");
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;
}