Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replaceing dots between characters

Tags:

python

regex

So I want to substitude dots in string when there is no space after of before the dots. I have thought this could be easily done with a regular expression but I haven't been able to do it.

I have patterns and I want them to be:

  • h.e.ll.o w.o.r.l.d: hello world
  • h.e.ll.o w.o.r.l.d: hello world
  • hello. world: hello. world

I have tried the following patterns:

\w+(\.)+\w+
\w+(\.+\w+)
\w+\.+\w+

I always get something like: he.ll.o wo.rl.d

I am using python's re module to match and replace with the following code:

>>> re.sub(r'\w+\.+\w+', lambda x: x.group(0).replace('.', ''), 'h.e.ll.o w.o.r.l.d')
'he.llo wo.rl.d'
like image 648
Jorge Rodriguez Molinuevo Avatar asked Mar 01 '26 01:03

Jorge Rodriguez Molinuevo


1 Answers

In all your patterns you consume a char after the dot, so there is no chance to match it in the next iteration with the first \w+ (as it must consume at least 1 word char).

To fix your approach, you may match 1+ word chars followed with 1+ repetitions of . followed with 1+ word chars:

re.sub(r'\w+(?:\.+\w+)*', lambda x: x.group(0).replace('.', ''), s)

Here is the Python demo.

Another approach to remove . between word chars is

re.sub(r'\b\.\b', '', s)

See this regex demo. Here, . is only matched in case it is within word chars.

Alternatively, you may use this approach to match any . not enclosed with whitespace:

re.sub(r'(?<!\s)\.(?!\s)', '', 'h.e.ll.o w.o.r.l.d')

See the Python demo and the regex demo.

Details

  • (?<!\s) - a negative lookbehind that fails the match if there is a whitespace immediately to the left of the current location
  • \. - a dot
  • (?!\s) - a negative lookahead that fails the match if there is a whitespace immediately to the right of the current location.
like image 122
Wiktor Stribiżew Avatar answered Mar 02 '26 13:03

Wiktor Stribiżew



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!