In Java, what is the best way to fix the missing whitespace after some punctuation marks like:
, . ; : ? !
For example:
String example = "This is!just an:example,of a string,that needs?to be fixed.by inserting:a whitespace;after punctuation marks.";
The output should be:
"This is! just an: example, of a string, that needs? to be fixed. by inserting: a whitespace; after punctuation marks."
It is clear that this does not work:
example = example.replaceAll("[,.!?;:]", " ");
So I'm looking for a solution waiting for your help. Thank you!!
You can use a combination of Positive Lookbehind and Negative Lookahead.
example = example.replaceAll("(?<=[,.!?;:])(?!$)", " ");
Explanation:
The Positive Lookbehind asserts at the position that follows any of the select punctuation. The use of the Negative Lookahead says, at this position ( end of the string ), the following can not match.
(?<= # look behind to see if there is:
[,.!?;:] # any character of: ',', '.', '!', '?', ';', ':'
) # end of look-behind
(?! # look ahead to see if there is not:
$ # before an optional \n, and the end of the string
) # end of look-behind
Working Demo
You have to add $0
to your replace expression, you can use:
example = example.replaceAll("[,.!?;:]", "$0 ");
It will replace your matched regex with that content plus a space.
Btw, if you want ensure that you don't have multiple whitespaces you can do:
example = example.replaceAll("[,.!?;:]", "$0 ").replaceAll("\\s+", " ");
Will convert:
This is!just an:example,of a string,that needs?to be fixed.by inserting:a whitespace;after punctuation marks.;
To:
This is! just an: example, of a string, that needs? to be fixed. by inserting: a whitespace; after punctuation marks.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With