I have a string that contains the following text
String my_string = "hello world. it's cold out brrrrrr! br br";
I’d like to replace each isolated br with <br />
The issue is that I’d like to avoid converting the string to
"hello world. it's cold out <br />rrrrr! <br /> <br />";
What I’d like to do is convert the string (using replaceAll) to
"hello world. it's cold out brrrrrr! <br /> <br />";
I’m sure this is very simple, but my regex isn’t correct.
my_string.replaceAll("\\sbr\\s|\\sbr$", "<br />");
my regex is supposed to find ‘whitespace’ ‘b’ ‘r’ ‘whitespace’ OR ‘whitespace’ ‘b’ ‘r’ ‘end of line’
but it misses the final “br” in my string
"hello world. it's cold out brrrrrr!<br />br"
what am I doing wrong?? TKS!
Use
Your regex doesn’t work because in
The pattern
\sbr\swill consume the whole␣br␣, leaving withnow there is no preceding space for this
brto match\sbr$, so it will be missed.On the other hand, the
\b, meaning a word-boundary, is a zero-width assertion, i.e. it won’t consume any characters. Therefore the spaces will be kept and all isolatedbr‘s will be matched.