Ved Gupta Ved Gupta - 8 months ago 38
Python Question

RegEx Python: Remove full stop from end of word but not in float

I want to know how to remove all periods occurring after the end of a word but not from float values in string.

import re
title= "Remove. dot from here but not from 4.334"
print title
title=re.sub(r'\.', '', title)
print title

This removes everything (.) from the strings. I want to remove only those which do not occur in between numbers or float.


You could use an assertion or two:

>>> re.sub(r'(?<!\d)\.(?!\d)', '', title)
'Remove dot from here but not from 4.334'

Precisely negative lookbehind and positive lookahead assertions