7
$\begingroup$

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."
$\endgroup$
  • 2
    $\begingroup$ 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", "...". $\endgroup$ – Kuba Oct 31 '18 at 9:29
  • $\begingroup$ @Kuba This is true. I will add some more details $\endgroup$ – james Oct 31 '18 at 9:30
  • $\begingroup$ BTW, I wonder the necessity of the braces (List). $\endgroup$ – Αλέξανδρος Ζεγγ Oct 31 '18 at 9:51
  • 1
    $\begingroup$ 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." $\endgroup$ – Alexey Popkov Oct 31 '18 at 9:56
  • $\begingroup$ @ΑλέξανδροςΖεγγ Good point. Yes, the text could also end and start with a number. Hmm...then it gets really difficult to solve, I guess. $\endgroup$ – james Oct 31 '18 at 9:58
8
$\begingroup$

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.

$\endgroup$
  • $\begingroup$ 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 ! $\endgroup$ – james Oct 31 '18 at 9:12
  • $\begingroup$ @james OK, let me add some explanations. $\endgroup$ – Αλέξανδρος Ζεγγ Oct 31 '18 at 9:13
  • $\begingroup$ Thank you very much for your explanation. $\endgroup$ – james Oct 31 '18 at 9:35
  • $\begingroup$ @james But you changed the problem? $\endgroup$ – Αλέξανδρος Ζεγγ Oct 31 '18 at 9:36
  • 2
    $\begingroup$ @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. $\endgroup$ – Veskah Oct 31 '18 at 22:58
8
$\begingroup$
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

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

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

$\endgroup$
5
$\begingroup$
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."}
$\endgroup$
  • $\begingroup$ Thanks a lot. Can you also try the new sample text that I added to the question ? $\endgroup$ – james Oct 31 '18 at 9:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.