Q:

Match tags with regular expression and strip them off in Java

I am trying to display blog excerpts as result of a search by tag. It would not be ideal at all having img or blockquote tags in those brief paragraphs, so I would like them to be stripped off, along with their content.
In order to do this my idea is to match with a regular expression the tags and then replace them with the empty string. So here it is my code:
protected static String removeTags(String s)
    {
        String reg = "<img .*>";
        String excerpt = s.replace(reg, "");
        System.out.println(excerpt);
        return excerpt;
    }

the code seems to work very well if in the regular expression do not appear symbols like *, <, >: in fact if my regular expression is merely img the code does its job just fine and it is taken off. I've tried to use equivalent patters such as \\w and others but still it seems they won't match the code. What am I doing wrong? 

A:

If you want to use regex in your match, you need to use replaceAll() instead of replace().
Also, you probably don't want to use .*, since * is a greedy quantifier and will consume everything up to the last > in your data. Try .*? (reluctant quantifier) or [^>]* instead.

