Ant
Ant

Reputation: 281

Regex and RSS Feed

i write a simple Rss Feed reader now i have this problem, in the item description i have the text but this text have this caracters <br/> for example

"my dog <br/> is black and he <br/> stay on table "

now i wont to clear the string from this caracters, i wirte this metod

private static String IsMatch(String s, String pattern) {
    try {
        Pattern patt = Pattern.compile(pattern);
        Matcher matcher = patt.matcher(s);
        return matcher.group();
    } catch (RuntimeException e) {
      return "Error";
    } }

and

String regex ="[<br/>]";

theString2=IsMatch(theString,regex);
 AppLog.logString(theString2);

but But this method return always Error. Can any one tell me what's the problem?

best regads Antonio

Upvotes: 0

Views: 1095

Answers (2)

zinking
zinking

Reputation: 5695

String regex ="(\<br\/\>)";

I think you need to pay attention to the grammar details about the regular expression: 1. escape the "<" like special characters

different language of regular expression have different specifications

Upvotes: 0

Guillaume Polet
Guillaume Polet

Reputation: 47637

The problem is that you never invoke find() on your matcher. You must invoke find() before invoking group() (and test that find() returns true).

I am not sure of what your method IsMatch is supposed to do. As it is, it will either return the match (ie, "<br/>", assuming you invoke find() before) either return "Error".

Also, don't put the brackets around <br/> in your regexp, they are not needed.

I wouls really consider using replace instead of regexp for your purposes:

String s = "my dog <br/> is black and he <br/> stay on table ".replace("<br/>","");

As a recommendation, don't catch an exception without logging the it. They provide valuable information that should not be hidden. It make debug really hard when a problem occurs.

Upvotes: 1

Related Questions