Remove periods at the end of sentences in python

后端 未结 3 1841
别那么骄傲
别那么骄傲 2021-01-21 01:16

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

相关标签:
3条回答
  • 2021-01-21 01:43

    How about

    pattern = re.compile(r'\.(\s)')
    wordList = pattern.sub(r'\1', wordList)
    

    This replaces a period followed by a space with just the space.

    0 讨论(0)
  • 2021-01-21 02:02

    Try a negative lookahead:

    \.(?!\d)
    

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

    0 讨论(0)
  • 2021-01-21 02:03

    In regex, the $ special character "[matches] the end of the string or just before the newline at the end of the string"

    In that case, assuming only one sentence per line, I would suggest the following:

    \.$
    

    This will match only periods that occur at the end of a string (or end of a line for multiline strings). Of course, if you cannot guarantee one sentence per line then they isn't the solution for you.

    0 讨论(0)
提交回复
热议问题