import re
def replace_basic():
string = "If the the problem is textual, use the the re module"
pattern = r"the the"
regexp = re.compile(pattern)
substituted = regexp.sub("the", string)
print("*** after replacement, the string is {0}".format(substituted))
def replace_advanced_func():
int_string = "1 2 3 4 5"
def int_match_to_float(match_obj):
return (match_obj.group('num') + ".0")
pattern = r"(?P<num>[0-9]+)"
regexp = re.compile(pattern)
substituted = regexp.sub(int_match_to_float, int_string)
print("**** after replacement, the string is {0}".format(substituted))
if __name__ == '__main__':
replace_basic()
replace_advanced_func()
As you can see from the above code, that we are passing a function to process the matched object, and we return a string which uses the matched object.
转载于:https://my.oschina.net/u/854138/blog/90398