Search and Replace in Python

One of the most important re methods that use regular expressions is sub.


re.sub(pattern, repl, string, max=0)

This method replaces all occurrences of the RE pattern in string with repl, substituting all occurrences unless max provided. This method returns modified string.


 Live Demo

import re
phone = "2004-959-559 # This is Phone Number"
# Delete Python-style comments
num = re.sub(r'#.*$', "", phone)
print "Phone Num : ", num
# Remove anything other than digits
num = re.sub(r'\D', "", phone)
print "Phone Num : ", num


When the above code is executed, it produces the following result −

Phone Num : 2004-959-559
Phone Num : 2004959559