L
L
LakeForest2021-08-14 12:56:42
Python
LakeForest, 2021-08-14 12:56:42

Did I write the regular expression correctly to remove the space before the punctuation mark?

Example:
a Greek was driving across a river. sees a Greek in the river cancer? put the hand of the Greek into the river, cancer by the hand of the Greek tsap!
Need to:
rode a Greek across the river. sees a Greek in the river rak? put the hand of the Greek into the river, cancer by the hand of the Greek tsap!

I wrote this: Do you think it's okay? How else can you?
re.sub("[^(\S)](?=[\.,?!])", r"\1", text)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
dodo512, 2021-08-14
@LakeForest

re.sub("\s(?=[.,?!])", "", text)
Or
re.sub("\s([.,?!])", r"\1", text)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question