Ved Gupta Ved Gupta - 1 year ago 98
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.

Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download