Octavia Togami
Octavia Togami

Reputation: 4296

Is Java's regex engine broken?

I am writing some code to remove parentheses and their contents from a String, but this line isn't working properly:

line.replaceAll("\\Q"+matchp+"\\E", rep);

This should replace all of the found group, say "(group) more words" and the regex matched "(group)", then the String would become rep + " more words". But the String is unaffected. Is quoting broken?

Upvotes: 0

Views: 148

Answers (1)

Ivaylo Strandjev
Ivaylo Strandjev

Reputation: 70929

replaceAll is not in-place do it like so:

line = line.replaceAll("\\Q"+matchp+"\\E", rep);

Upvotes: 11

Related Questions