-1

I have a file with below contents:

Test:Line1@line.com
Goal:[[goal]]
Email:[[email]]
Further Text

Below is code to get the file contents into a StringBuffer and get the value into the String output.

Path signature = Paths.get("signature.txt");
StringBuffer sigBuffer = new StringBuffer();
String line = null;
BufferedReader reader = null;
try {
 reader = Files.newBufferedReader(signature);
 while ((line = reader.readLine()) != null) {
   sigBuffer.append(line);
   sigBuffer.append("\n");
 }
 String output = sigBuffer.toString();
} catch...finally...

I want to replace the [[goal]] with Goal A AND [[email]] with email@mail.com

I tried this:

String regExp = "\\[\\[[a-z_]*\\]\\]";
output.replaceFirst(regExp, "Goal A");
output.replaceFirst(regExp, "email@mail.com");

But my output is still:

Test:Line1@line.com
Goal:[[goal]]
Email:[[email]]
Further Text
rapidclock
  • 1,677
  • 2
  • 17
  • 32

1 Answers1

2

String is immutable. You need to assign the result of the replaceFirst call back to the variable.

output = output.replaceFirst(....)
Andy Turner
  • 137,514
  • 11
  • 162
  • 243