Java – How to replace strings if not included between ‘<' and '>‘

How to replace strings if not included between ‘<' and '>‘… here is a solution to the problem.

How to replace strings if not included between ‘<' and '>‘

I have to replace part of the text, but only if its substring doesn’t contain between “”.

For example, if I have the following text

<text color='blue'>My jeans are red</text>
<text color='red'>I am wearing a red t-shirt</text>
<text color='yellow'>I like red fruits</text>

I want to replace the word “

red” with another word, how can I replace the word in this text without replacing the word between “”?
I tried to write a regular expression for this, but I didn’t succeed….

I

think a stupid way to do this is to analyze all the text (character by character) and see if I’m inside or outside <...>, if I’m outside… I think there should be a smarter way to replace text!

Solution

Is this appropriate for you?

If you only want to replace in a single row:

final String s = "<text color='red'>I am wearing a red t-shirt</color>";
        System.out.println(s.replaceAll("(?<=>)(.*?) red", "$1blue"));

Will be printed

<text color='red'>I am wearing a blue t-shirt</color>

Multi-line case:

final String s = "<text color='red'>I am wearing a red t-shirt</color>\n<text color='red'>You are wearing a red T-shirt</color>";
        System.out.println(s.replaceAll("(?m)^(.*?) (?<=>) ([^>]*?) red", "$1$2blue"));

Output:

<text color='red'>I am wearing a blue t-shirt</color>
<text color='red'>You are wearing a blue T-shirt</color>

Related Problems and Solutions