Search code examples
pythonregexsentence

Remove periods at the end of sentences in python


I have sentences like this - "this is a test. 4.55 and 5,000." I want to remove the period at the end of the sentences, but not between numbers. My output has to be - "this is a test 4.55 and 5,000" I tried the below options, but not getting the required output:

wordList = "this is a test. 4.55 and 5,000."
pattern3 = re.compile("[^\w\d]+")
wordList = pattern3.sub(' ',wordList)

Also tried the below 2:

pattern3 = re.compile("[^\w]|^[0-9]\.[0-9]")
pattern3 = re.compile("[^\w]|^([0-9]/.[0-9]+)")

I don't know where I am going wrong. Can someone give me some pointers? I searched the earlier posts and tried them, but they are not working for my situation.


Solution

  • Try a negative lookahead:

    \.(?!\d)
    

    What this matches is any period that's not followed by a digit.