下面的python代码先清理字符串中的非电话号码部分,然后又清除了电话号码中的非数字字符
#!/usr/bin/python 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
执行结果如下
Phone Num : 2004-959-559 Phone Num : 2004959559