Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert a whitespace after comma, period and other punctuation marks

Tags:

java

string

regex

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!!

like image 346
MariaH Avatar asked Dec 07 '22 01:12

MariaH


2 Answers

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

like image 167
hwnd Avatar answered Jan 20 '23 00:01

hwnd


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.

like image 32
Federico Piazza Avatar answered Jan 19 '23 22:01

Federico Piazza