Dustin
Dustin

Reputation: 1393

Java replace regex before and after a period

I am working with XML on an android app that sometimes leaves sentences bumped up against each other.

Like: First sentence.Another sentence

I know I need to use [a-z] (lowercase letters), [A-Z] (uppercase letters), and all digits ([0-9]?) to search before and after the period, and then add a space after the period.

Maybe something like:

myString = myString.replaceAll("(\\p{Ll})(\\p{Lu})", "$1 $2");

My searches and efforts have been useless so far, so any and all help is welcomed. Thanks

Upvotes: 1

Views: 643

Answers (1)

Tim Pietzcker
Tim Pietzcker

Reputation: 336158

You were almost there, you just forgot to match the dot:

myString = myString.replaceAll("(\\p{Ll})\\.(\\p{Lu})", "$1. $2");

And since you're not actually doing anything with the letter before and after the dot, you can speed things up a bit by using lookaround assertions:

myString = myString.replaceAll("(?<=\\p{Ll})\\.(?=\\p{Lu})", ". ");

Upvotes: 3

Related Questions