Hello again :) I'm trying to define a simple "spelling correction" function that takes a string and sees to it that 1) two or more occurrences of the space character is compressed into one, and 2) inserts an extra space after a period if the period is directly followed by a letter. My piece of code is not working. Please advise.

import re
def correct(s):
  rem = re.sub('\+','',s)    # hoping that this remove extra spaces.
  #rem = re.sub('\.','. ',rem)  # and this inserting extra spaces after a period.

  print rem
s = raw_input('input a weird string: ')
correct(s)

A regex to match more than 1 space character is

r' {2,}'

To match a dot followed by a letter without consuming the letter, you can use

r'\.(?=[a-zA-Z])'

Always use raw strings(r'...') with regexes, in order to preserve \ characters.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.