Question

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.

Était-ce utile?

La solution

Try a negative lookahead:

\.(?!\d)

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

Autres conseils

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.

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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top