2016-12-11

Python - Remove multiple contiguous characters, leaving only one

re.sub( ur"([-])\1+", r"\1", string_here )

Where - is the character being sought.

print re.sub(ur"([-])\1+", r"\1", "111-112,120----130,1-1,2--2,3---3,4----4,5-----5,8" )
111-112,120-130,1-1,2-2,3-3,4-4,5-5,8

No comments:

Post a Comment