2

Problem statement:

Inserts an extra space after a period if the period is directly followed by a letter.

Below is code:

string="This   is  very funny  and    cool.Indeed!"

re.sub("\.[a-zA-Z]", ". ", string)

and output:

"This is very funny and cool. ndeed!"

It is replacing the first character after '.'.

Any solution for this?

1
  • Try using a capturing group Commented Sep 25, 2014 at 13:41

2 Answers 2

3

You can use positivie lookahead assertion, which does not consume the matched part:

>>> re.sub(r"\.(?=[a-zA-Z])", ". ", string)
'This   is  very funny  and    cool. Indeed!'

Alternative using capturing group and backreference:

>>> re.sub(r"\.([a-zA-Z])", r". \1", string)  # NOTE - r"raw string literal"
'This   is  very funny  and    cool. Indeed!'

FYI, you can use \S instead of [a-zA-Z] to match non-space characters.

Sign up to request clarification or add additional context in comments.

Comments

0

You could also use both lookahead and lookbehind in your regex.

>>> import re
>>> string="This   is  very funny  and    cool.Indeed!"
>>> re.sub(r'(?<=\.)(?=[A-Za-z])', r' ', string)
'This   is  very funny  and    cool. Indeed!'

OR

You may use \b,

>>> re.sub(r'(?<=\.)\b(?=[A-Za-z])', r' ', string)
'This   is  very funny  and    cool. Indeed!'

Explanation:

  • (?<=\.) Just look after to a literal dot.
  • (?=[A-Za-z]) Asserts that the matched boundary must be followed by a letter.
  • If yes, then replace the boundary with space.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.