0

I want to remove all the text between < and >. I am trying to get familiar with regular expressions so my code for this looks like:

line.replaceAll("<.*?>","");

I am replacing it line by line using a recursive method.

This is the whole method to clear things up. I am getting the same input and output.

    // recursively goes through the string and removes anything surrounded by "< >"
public static void removeTags(String line) {
    line.replaceAll("<.*?>","");
    cleanString = cleanString + line;
    if (sc.hasNext()) {
        removeTags(sc.nextLine());
    }
}
3
  • 2
    Strings are immutable. replaceAll doesn't affect original string but creates new one. Commented Mar 19, 2015 at 19:29
  • Can you provide any example of input and output you getting ? Commented Mar 19, 2015 at 19:31
  • Oh okay. Pshemo has it. My problem is that I am essentially creating a new blank string Commented Mar 19, 2015 at 19:35

1 Answer 1

3

You have to store your resulting string, so change this:

line.replaceAll("<.*?>","");

To

line = line.replaceAll("<.*?>","");
Sign up to request clarification or add additional context in comments.

1 Comment

yupp. I had it right... i feel dumb now, for now actually storing the resulting string in the variable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.