Friday, 15 February 2013

java - Insert a whitespace after comma, period and other punctuation marks -



java - Insert a whitespace after comma, period and other punctuation marks -

in java, best way prepare missing whitespace after punctuation marks like:

, . ; : ? !

for example:

string illustration = "this is!just an:example,of string,that needs?to fixed.by inserting:a whitespace;after punctuation marks.";

the output should be:

"this is! an: example, of string, needs? fixed. inserting: whitespace; after punctuation marks."

it clear not work:

example = example.replaceall("[,.!?;:]", " ");

so i'm looking solution waiting help. give thanks you!!

you have add together $0 replace expression, can use:

example = example.replaceall("[,.!?;:]", "$0 ");

it replace matched regex content plus space.

btw, if want ensure don't have multiple whitespaces can do:

example = example.replaceall("[,.!?;:]", "$0 ").replaceall("\\s+", " ");

will convert:

this is!just an:example,of string,that needs?to fixed.by inserting:a whitespace;after punctuation marks.;

to:

this is! an: example, of string, needs? fixed. inserting: whitespace; after punctuation marks.

java regex string

No comments:

Post a Comment