Mirrana
Mirrana

Reputation: 1731

String.replaceAll() and Regular Expressions

I'm trying to search a bunch of lines of text and replace the first word in the sentence with a bolded version of that word. What I thought was the appropriate code isn't working. What am I doing wrong?

String s;
s.replaceAll("^(.+)\\B", "<b>\\1</b>")

I'm wondering if this is even the proper approach, because my string is one ling HTML string with each line ending in <br>.. so there's really only one "line". Not sure how to accomplish this given this insight.

Upvotes: 1

Views: 95

Answers (1)

arshajii
arshajii

Reputation: 129497

Strings are immutable!!

s = s.replaceAll("^(.+)\\B", "<b>\\1</b>");

Oh, and you should use $0 to refer to the match as opposed to \\1.

Upvotes: 5

Related Questions