I'm dealing with text formatting from a plaintext message (HL7) and reformatting it for display. An example of one is \.sp5\
. This means put in five line breaks.
So I'm thinking I would want to do something like this:
Pattern.compile("\\\.sp(\d+)\\").matcher(retval).replaceAll("\n{$1}");
My IDE is telling me that there is an invalid escape sequence at \d
and I am not sure if the replaceAll argument will do what I expect. I think that regular expression is describing "backslash dot s p one-or-more-digits backslash" and I want the replacement to say "put in $1 line breaks".
How can I accomplish this?
The solution was a combination from two commenters below:
Pattern verticalSpacesPattern = Pattern.compile("\\\\\\.sp(\\d+)\\\\", Pattern.MULTILINE);
Matcher verticalSpacesMatcher = verticalSpacesPattern.matcher(retval);
while (verticalSpacesMatcher.find()) {
int lineBreakCount = Integer.parseInt(verticalSpacesMatcher.group(1));
String lineBreaks = StringUtils.repeat("\n", lineBreakCount);
String group = verticalSpacesMatcher.group(0);
retval = StringUtils.replace(retval, group, lineBreaks);
}