Here is a way to combine 2 replacements in single String#replaceAll
call:
String repl = data.replaceAll("(?s)(.*?example\\s*)|[\r\n]+", "|");
//=> |for the problem(s)|I|am having|while|working on|the issue-101
- Only caveat is that it will have a starting pipe character in the output.
(?s)
is for DOTALL to make dot match newlines also.