Don't worry, "$" works exactly the way as it is expected. If the flag Pattern.MULTILINE is set, then "$" matches both the line terminator and the end of the stream. If the flag is not set, then it matches only the end of the stream.
At the end of my post you will find two unit tests that document the behaviour.
I very much apologize for the late reply. Your post was inadvertently moved to the wrong folder. By the way, I will add your question to a new FAQ wiki page.
public void testRemovalAtTheEndOfStream_notUsingMultiLineFlag()
throws Exception {
String startOfStream = StringUtils.repeat("]}}", 10000);
String entireStartOfString = startOfStream + "]}}\n" + startOfStream
+ "]}}\n" + startOfStream;
Reader originalReader = new StringReader(entireStartOfString + "]}}");
Modifier myModifier = new RegexModifier(Pattern.quote("]}}") + "$", 0,
"", 0, 3);
Reader modifyingReader = new ModifyingReader(originalReader, myModifier);
String output = IOUtils.toString(modifyingReader);
assertEquals(entireStartOfString, output);
}
public void testRemovalAtTheEndOfLine_usingMultiLineFlag() throws Exception {
String startOfStream = StringUtils.repeat("]}}", 3);
Reader originalReader = new StringReader(startOfStream + "]}}\n"
+ startOfStream + "]}}\n" + startOfStream + "]}}");
Modifier myModifier = new RegexModifier(Pattern.quote("]}}") + "$",
Pattern.MULTILINE, "", 0, 3);
Reader modifyingReader = new ModifyingReader(originalReader, myModifier);
String output = IOUtils.toString(modifyingReader);
assertEquals(startOfStream + "\n" + startOfStream + "\n"
+ startOfStream, output);
}