joe nesbitt wrote:I tried stringxxx.replaceAll("\\r+.", "\\X000d ") for \r. But not sure how to replace only a particular \r I tried this but in vain:
stringxxx.replaceAll("|*\\r+.*|", "\\X000d ")
String.replaceAll uses regular expressions. | has special meaning in regular expressions. Also, both your attempts would also remove the . (any character) / .* (all characters); that's not what you want, is it?
Check out the Javadoc of java.util.regex.Pattern and check for "positive lookahead" and "positive lookbehind".
However, I don't think using simple regular expressions will help you out here. How would your example
be different from the following if you'd use only regular expressions:
or even
All represent the same characters. Is it really the number of | characters? If so then using a simple loop would probably be better. In pseudo code:
You'll probably want a StringBuilder to store the modified file contents.