# Add a space after a ending dot of a sentence

How can I add a space after the ending dot of a sentence? So dots like in "1.2" should be unaffected. Also if there is already a space, then there should not be a space added.

text1 = "This is a sample text. Just 1.2 to test.To add a space after the dot...Okay."


Result desired:

"This is a sample text. Just 1.2 to test. To add a space after the dot... Okay. "


EDIT: As pointed out in the comments: The text could also end and start with a number:

text2 = "I watched football.10 people played in 2 teams, my friend was player number 7. 20 minutes later the game ended with the score 2:1.Then I went home."

• Just for answerers sake, can you be more precise about the rules? Clearly not each dot is affected. And other exceptions (or not) come to my mind too e.g. "1.2", "...". – Kuba Oct 31 '18 at 9:29
• @Kuba This is true. I will add some more details – james Oct 31 '18 at 9:30
• BTW, I wonder the necessity of the braces (List). – Αλέξανδρος Ζεγγ Oct 31 '18 at 9:51
• Can your text contain sentences ending with a number? Starting with a number? Like this: "I watched football.10 people played in 2 teams, my friend was player number 7. 20 minutes later the game ended." – Alexey Popkov Oct 31 '18 at 9:56
• @ΑλέξανδροςΖεγγ Good point. Yes, the text could also end and start with a number. Hmm...then it gets really difficult to solve, I guess. – james Oct 31 '18 at 9:58

## 4 Answers

For latest requirement:

rule = RegularExpression["(?!\\d\\.\\d)(\\w\\.+)(?! )(\\w?)"] -> "$$1$$2";
StringReplace[rule] @ {text1, text2}

{"This is a sample text. Just 1.2 to test. To add a space after the dot... Okay. ",
"I watched football. 10 people played in 2 teams, my friend was player number 7. 20 minutes later the game ended with the score 2:1. Then I went home. "}


"Negative Lookahead" ("(?!...)") is used, BTW.

Older-er-er response

StringReplace[text, RegularExpression["(\\w\\.)(\\w)"] -> "$$1$$2"]


"\\w" means word characters, including letters, digits and the underscore _. "\\." means a period/dot literally. So the regular expression means to find a string pattern with length three: a word character followed by a period and followed by a word character.

Parentheses mean a group, and "\$n" where n is an integer represents the contents in the n-th group.

So the whole operation is to add a blank between the two groups after locating them by the string pattern.

• Awesome ! Thanks a lot ! Could you just add one or two comments about the terms withing the RegularExpression function ? I don't quite understand how you did it. Thanks ! – james Oct 31 '18 at 9:12
• @james OK, let me add some explanations. – Αλέξανδρος Ζεγγ Oct 31 '18 at 9:13
• Thank you very much for your explanation. – james Oct 31 '18 at 9:35
• @james But you changed the problem? – Αλέξανδρος Ζεγγ Oct 31 '18 at 9:36
• @james Lookahead does what it sounds like in Regex. It looks ahead to see if the next set of characters match its pattern (negative lookahead sees if it doesn't) without actually capturing any characters. In the example, it lets you check if the character after a . is a space, or that you aren't trying to split 1.2. – Veskah Oct 31 '18 at 22:58
StringReplace[text, "." ~~a:Except[DigitCharacter|WhitespaceCharacter|"."] :> ". "<> a]


{"This is a sample text. Just 1.2 to test. To add a space after the dot... Okay."}

Also

StringReplace[text, a:LetterCharacter|"."~~"." ~~b:LetterCharacter:> a<>". "<>b ]


same result

• Thanks a lot. Can you also try the new sample text that I added to the question ? – james Oct 31 '18 at 9:35
• @james, just did. – kglr Oct 31 '18 at 9:39
StringReplace[StringReplace[text, "." -> ". "], ". " ~~ EndOfString -> "."]


{"This is a sample text. Just to test. To add a space after the dot. Okay."}

text= {"This is a sample text.Just to test.To add a space after the dot.Okay."}

StringReplace[text,
WordBoundary ~~ "." ~~ Except[WhitespaceCharacter, WordBoundary] :> ". "]

{"This is a sample text. Just to test. To add a space after the dot. Okay."}


Or

StringReplace[text, a : WordCharacter ~~ "." ~~ b : WordCharacter :> a <> ". " <> b]

{"This is a sample text. Just to test. To add a space after the dot. Okay."}


Both solutions correctly handle the cases when there already is a whitespace character after the dot:

text2 = {"This is a sample text. Just to test.To add a space after the dot. Okay."};

StringReplace[text2, a : WordCharacter ~~ "." ~~ b : WordCharacter :> a <> ". " <> b]

{"This is a sample text. Just to test. To add a space after the dot. Okay."}

• Thanks a lot. Can you also try the new sample text that I added to the question ? – james Oct 31 '18 at 9:35