Replace one part of a pattern in a string/sentence?

Viewed 81

There is a text blob for example

"Text blob1. Text blob2. Text blob3 45.6%. Text blob4."

I want to replace the dots i.e. "." with space " ". But at the same time, dots appearing between numbers should be retained. For example, the previous example should be converted to:

"Text blob1 Text blob2 Text blob3 45.6% Text blob4"

If I use:

p = re.compile('\.')
s = p.sub(' ', s)

It replaces all dots with space.

Any suggestions on what pattern or method works here?

3 Answers

Use

\.(?!(?<=\d\.)\d)

See proof. This expression will match any dot that has no digit after it that is preceded with a digit and a dot.

EXPLANATION

NODE                     EXPLANATION
--------------------------------------------------------------------------------
  \.                       '.'
--------------------------------------------------------------------------------
  (?!                      look ahead to see if there is not:
--------------------------------------------------------------------------------
    (?<=                     look behind to see if there is:
--------------------------------------------------------------------------------
      \d                       digits (0-9)
--------------------------------------------------------------------------------
      \.                       '.'
--------------------------------------------------------------------------------
    )                        end of look-behind
--------------------------------------------------------------------------------
    \d                       digits (0-9)
--------------------------------------------------------------------------------
  )                        end of look-ahead

You might not need regex here. Replace a dot-space with a space.

s.replace('. ', ' ')

That isn't good enough if you have any periods followed by a newline or that terminate the string, but you still wouldn't need a regex:

s.replace('. ', ' ').replace('.\n', '\n').rstrip('.')

Suppose the string were

A.B.C blob3 45.6%. Text blob4.

Match all periods other than those both preceded and followed by a digit

If after replacements, the string

A B C blob3 45.6% Text blob4

were desired, one could use re.sub with the regular expression

r'(?<!\d)\.|\.(?!\d)'

to replace matches of periods with empty strings.

The regex reads, "match a period that is not preceded by a character other than a digit or is not followed by a character other than a digit".

Demo 1

The double-negative is employed to match a period at the beginning or end of the string. One could instead use the logical equivalent:

r'(?<=^|\D)\.|\.(?=\D|$)'

Match all periods except those both preceded and followed by a whitespace character

On the other hand, if, after substitutions, the string

A.B.C blob3 45.6% Text blob4

were desired one could use re.sub with the regular expression

r'(?<!\S)\.|\.(?!\S)'

to replace matches of periods with empty strings.

This regex reads, "match a period that is not preceded by a character other than a whitespace or is not followed by a character other than a whitespace".

Demo 2

One could instead use the logical equivalent:

r'(?<=^|\s)\.|\.(?=\s|$)'
Related