Add a space after a ending dot of a sentence
Clash Royale CLAN TAG#URR8PPP
up vote
3
down vote
favorite
How can I add a space after the ending dot of a sentence ? So dots like "1.2" should be unaffected. Also if there is already a space, then there should not be a space added.
text= "This is a sample text. Just 1.2 to test.To add a space after the dot...Okay."
Result:
text= "This is a sample text. Just 1.2 to test. To add a space after the dot... Okay."
string-manipulation
add a comment |Â
up vote
3
down vote
favorite
How can I add a space after the ending dot of a sentence ? So dots like "1.2" should be unaffected. Also if there is already a space, then there should not be a space added.
text= "This is a sample text. Just 1.2 to test.To add a space after the dot...Okay."
Result:
text= "This is a sample text. Just 1.2 to test. To add a space after the dot... Okay."
string-manipulation
1
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♦
11 mins ago
@Kuba This is true. I will add some more details
– james
10 mins ago
add a comment |Â
up vote
3
down vote
favorite
up vote
3
down vote
favorite
How can I add a space after the ending dot of a sentence ? So dots like "1.2" should be unaffected. Also if there is already a space, then there should not be a space added.
text= "This is a sample text. Just 1.2 to test.To add a space after the dot...Okay."
Result:
text= "This is a sample text. Just 1.2 to test. To add a space after the dot... Okay."
string-manipulation
How can I add a space after the ending dot of a sentence ? So dots like "1.2" should be unaffected. Also if there is already a space, then there should not be a space added.
text= "This is a sample text. Just 1.2 to test.To add a space after the dot...Okay."
Result:
text= "This is a sample text. Just 1.2 to test. To add a space after the dot... Okay."
string-manipulation
string-manipulation
edited 10 mins ago
asked 34 mins ago


james
691518
691518
1
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♦
11 mins ago
@Kuba This is true. I will add some more details
– james
10 mins ago
add a comment |Â
1
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♦
11 mins ago
@Kuba This is true. I will add some more details
– james
10 mins ago
1
1
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♦
11 mins ago
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♦
11 mins ago
@Kuba This is true. I will add some more details
– james
10 mins ago
@Kuba This is true. I will add some more details
– james
10 mins ago
add a comment |Â
4 Answers
4
active
oldest
votes
up vote
1
down vote
StringReplace[StringReplace[text, "." -> ". "], ". " ~~ EndOfString -> "."]
"This is a sample text. Just to test. To add a space after the dot. Okay."
add a comment |Â
up vote
1
down vote
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
6 mins ago
add a comment |Â
up vote
1
down vote
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."
Thanks a lot. Can you also try the new sample text that I added to the question ?
– james
6 mins ago
@james, just did.
– kglr
2 mins ago
add a comment |Â
up vote
1
down vote
StringReplace[text, RegularExpression["(\w\.)(\w)"] -> "$1 $2"]
"\w"
means word characters, including letters, digits and the underscore _
. "\."
means a period 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 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.
Update for the new string:
StringReplace[text, RegularExpression["([a-zA-Z]\.+)([a-zA-Z])"] -> "$1 $2"]
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
28 mins ago
@james OK, let me add some explanations.
– Î‘λÎÂξανδÃÂο Ζεγγ
28 mins ago
Thank you very much for your explanation.
– james
6 mins ago
@james But you changed the problem?
– Î‘λÎÂξανδÃÂο Ζεγγ
5 mins ago
Kuba's comment made me realize that there is actually more to the question that I thought, and that I need to be more specific.
– james
4 mins ago
 |Â
show 1 more comment
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
StringReplace[StringReplace[text, "." -> ". "], ". " ~~ EndOfString -> "."]
"This is a sample text. Just to test. To add a space after the dot. Okay."
add a comment |Â
up vote
1
down vote
StringReplace[StringReplace[text, "." -> ". "], ". " ~~ EndOfString -> "."]
"This is a sample text. Just to test. To add a space after the dot. Okay."
add a comment |Â
up vote
1
down vote
up vote
1
down vote
StringReplace[StringReplace[text, "." -> ". "], ". " ~~ EndOfString -> "."]
"This is a sample text. Just to test. To add a space after the dot. Okay."
StringReplace[StringReplace[text, "." -> ". "], ". " ~~ EndOfString -> "."]
"This is a sample text. Just to test. To add a space after the dot. Okay."
answered 25 mins ago
Chris Degnen
21.5k23382
21.5k23382
add a comment |Â
add a comment |Â
up vote
1
down vote
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
6 mins ago
add a comment |Â
up vote
1
down vote
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
6 mins ago
add a comment |Â
up vote
1
down vote
up vote
1
down vote
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."
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."
edited 8 mins ago
answered 22 mins ago
Alexey Popkov
37.9k4104259
37.9k4104259
Thanks a lot. Can you also try the new sample text that I added to the question ?
– james
6 mins ago
add a comment |Â
Thanks a lot. Can you also try the new sample text that I added to the question ?
– james
6 mins ago
Thanks a lot. Can you also try the new sample text that I added to the question ?
– james
6 mins ago
Thanks a lot. Can you also try the new sample text that I added to the question ?
– james
6 mins ago
add a comment |Â
up vote
1
down vote
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."
Thanks a lot. Can you also try the new sample text that I added to the question ?
– james
6 mins ago
@james, just did.
– kglr
2 mins ago
add a comment |Â
up vote
1
down vote
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."
Thanks a lot. Can you also try the new sample text that I added to the question ?
– james
6 mins ago
@james, just did.
– kglr
2 mins ago
add a comment |Â
up vote
1
down vote
up vote
1
down vote
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."
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."
edited 4 mins ago
answered 15 mins ago
kglr
169k8192395
169k8192395
Thanks a lot. Can you also try the new sample text that I added to the question ?
– james
6 mins ago
@james, just did.
– kglr
2 mins ago
add a comment |Â
Thanks a lot. Can you also try the new sample text that I added to the question ?
– james
6 mins ago
@james, just did.
– kglr
2 mins ago
Thanks a lot. Can you also try the new sample text that I added to the question ?
– james
6 mins ago
Thanks a lot. Can you also try the new sample text that I added to the question ?
– james
6 mins ago
@james, just did.
– kglr
2 mins ago
@james, just did.
– kglr
2 mins ago
add a comment |Â
up vote
1
down vote
StringReplace[text, RegularExpression["(\w\.)(\w)"] -> "$1 $2"]
"\w"
means word characters, including letters, digits and the underscore _
. "\."
means a period 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 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.
Update for the new string:
StringReplace[text, RegularExpression["([a-zA-Z]\.+)([a-zA-Z])"] -> "$1 $2"]
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
28 mins ago
@james OK, let me add some explanations.
– Î‘λÎÂξανδÃÂο Ζεγγ
28 mins ago
Thank you very much for your explanation.
– james
6 mins ago
@james But you changed the problem?
– Î‘λÎÂξανδÃÂο Ζεγγ
5 mins ago
Kuba's comment made me realize that there is actually more to the question that I thought, and that I need to be more specific.
– james
4 mins ago
 |Â
show 1 more comment
up vote
1
down vote
StringReplace[text, RegularExpression["(\w\.)(\w)"] -> "$1 $2"]
"\w"
means word characters, including letters, digits and the underscore _
. "\."
means a period 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 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.
Update for the new string:
StringReplace[text, RegularExpression["([a-zA-Z]\.+)([a-zA-Z])"] -> "$1 $2"]
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
28 mins ago
@james OK, let me add some explanations.
– Î‘λÎÂξανδÃÂο Ζεγγ
28 mins ago
Thank you very much for your explanation.
– james
6 mins ago
@james But you changed the problem?
– Î‘λÎÂξανδÃÂο Ζεγγ
5 mins ago
Kuba's comment made me realize that there is actually more to the question that I thought, and that I need to be more specific.
– james
4 mins ago
 |Â
show 1 more comment
up vote
1
down vote
up vote
1
down vote
StringReplace[text, RegularExpression["(\w\.)(\w)"] -> "$1 $2"]
"\w"
means word characters, including letters, digits and the underscore _
. "\."
means a period 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 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.
Update for the new string:
StringReplace[text, RegularExpression["([a-zA-Z]\.+)([a-zA-Z])"] -> "$1 $2"]
StringReplace[text, RegularExpression["(\w\.)(\w)"] -> "$1 $2"]
"\w"
means word characters, including letters, digits and the underscore _
. "\."
means a period 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 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.
Update for the new string:
StringReplace[text, RegularExpression["([a-zA-Z]\.+)([a-zA-Z])"] -> "$1 $2"]
edited 3 mins ago
answered 31 mins ago


ΑλÎÂξανδÃÂο Ζεγγ
3,0991927
3,0991927
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
28 mins ago
@james OK, let me add some explanations.
– Î‘λÎÂξανδÃÂο Ζεγγ
28 mins ago
Thank you very much for your explanation.
– james
6 mins ago
@james But you changed the problem?
– Î‘λÎÂξανδÃÂο Ζεγγ
5 mins ago
Kuba's comment made me realize that there is actually more to the question that I thought, and that I need to be more specific.
– james
4 mins ago
 |Â
show 1 more comment
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
28 mins ago
@james OK, let me add some explanations.
– Î‘λÎÂξανδÃÂο Ζεγγ
28 mins ago
Thank you very much for your explanation.
– james
6 mins ago
@james But you changed the problem?
– Î‘λÎÂξανδÃÂο Ζεγγ
5 mins ago
Kuba's comment made me realize that there is actually more to the question that I thought, and that I need to be more specific.
– james
4 mins ago
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
28 mins ago
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
28 mins ago
@james OK, let me add some explanations.
– Î‘λÎÂξανδÃÂο Ζεγγ
28 mins ago
@james OK, let me add some explanations.
– Î‘λÎÂξανδÃÂο Ζεγγ
28 mins ago
Thank you very much for your explanation.
– james
6 mins ago
Thank you very much for your explanation.
– james
6 mins ago
@james But you changed the problem?
– Î‘λÎÂξανδÃÂο Ζεγγ
5 mins ago
@james But you changed the problem?
– Î‘λÎÂξανδÃÂο Ζεγγ
5 mins ago
Kuba's comment made me realize that there is actually more to the question that I thought, and that I need to be more specific.
– james
4 mins ago
Kuba's comment made me realize that there is actually more to the question that I thought, and that I need to be more specific.
– james
4 mins ago
 |Â
show 1 more comment
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f184998%2fadd-a-space-after-a-ending-dot-of-a-sentence%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
1
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♦
11 mins ago
@Kuba This is true. I will add some more details
– james
10 mins ago