简体   繁体   中英

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM