Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

299
Views
How to add missing spaces after periods using regex, without changing decimals

I have a large piece of text that is missing spaces after some of the periods. However the text also contains decimal numbers.

Here's what I have so far to fix the problem using regex (I'm using python):

re.sub(r"(?!\d\.\d)(?!\. )\.", '. ', my_string)

But the first escape group doesn't seem to work. It still matches periods in decimal numbers.

Here is sample text to make sure any potential solution works:

this is a.match
this should also match.1234
and this should 123.match

this should NOT match. Has space after period
this also should NOT match 1.23
over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

You can use

re.sub(r'\.(?!(?<=\d\.)\d) ?', '. ', text)

See the regex demo. The trailing space is matched optionally, so if it is there, it will be removed and put back.

Details

  • \. - a dot
  • (?!(?<=\d\.)\d) - do not match any further if the dot before was a dot between two digit
  • ? - an optional space.

See a Python demo:

import re
text = "this is a.match\nthis should also match.1234\nand this should 123.match\n\nthis should NOT match. Has space after period\nthis also should NOT match 1.23"
print(re.sub(r'\.(?!(?<=\d\.)\d) ?', '. ', text))

Output:

this is a. match
this should also match. 1234
and this should 123. match

this should NOT match. Has space after period
this also should NOT match 1.23

Alternatively, use a (?! ) lookahead as in your attempt:

re.sub(r'\.(?!(?<=\d\.)\d)(?! )', '. ', text)

See the regex demo and the Python demo.

over 4 years ago · Santiago Trujillo Report

0

Another way.. not sure if this is better or worse for performance than Wiktor's solution.

re.sub(r"(?!\d\.\d)(?!.\. )(.\.)(.)", r"\1 \2", my_string)
over 4 years ago · Santiago Trujillo Report

0

txt="hello world.this is boise idaho.a this is twin falls."
pattern=r"(\w+\s*\.\w+)+"
matches=re.findall(pattern,txt)
for item in matches:
    front,back=item.split('.')
    replace=front+'. '+back
    txt=re.sub(item,replace,txt)
    
print(txt)

hello world. this is boise idaho. a this is twin falls.
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!