python正则实例
# -*- coding: cp936 -*-
import re
idcardregex=r"^[1-9]\d{14}(\d{2}[0-9x])?$"
print re.search(idcardregex,"110101198001017016")!=None
print re.search(idcardregex,"1101018001017016")!=None
print re.search(idcardregex,"11010119800101701x")!=None
print "month tags:"
print re.search(r"(0?[1-9]|1[012])","11")!=None
print re.search(r"(0?[1-9]|1[012])","08")!=None
print "month:"
print re.search(r"(0?[1-9]|1[012])","11")!=None
print "day:"
print re.search(r"(0?[1-9]|[12]\d|3[01])","28")!=None
print "hour:"
print re.search(r"(0?[1-9]|[01]\d|2[0-4])","28")!=None
print "minute:"
print re.search(r"(0?[1-9]|[0-5]\d|60)","71")!=None
print "year-month-day"
print re.search(r"(\d{4})-(\d{2})-(\d{2})","2013-12-08")!=None
print re.search(r"(\d{4})-(\d{2})-(\d{2})","2013-12-08").group(0)
print re.sub(r"(\d{4})-(\d{2})-(\d{2})",r"\2/\3/\1","2013-12-08")
print re.sub(r"(\d{4})-(\d{2})-(\d{2})",r"\1年\2月\3日","2013-12-08")
print "复杂tag:"
pairedTagRegex=r"<([a-zA-Z0-9]+)(\s[^>]+)?>[\s\S]*?</\1>"
print re.search(pairedTagRegex,"<bold>text</bold>")!=None
print re.search(pairedTagRegex,"<span class=\"class1\">text</span>")!=None
print "匹配IP其中一段的表达式"
segment=r"(0{0,2}[0-9]|0?[0-9]{2}|1[0-9][0-9]|2[0-4][0-9]|25[0-5])"
print "正确的IP表达式"
ipAddressRegex=r"(" + segment + r"\.){3}" +segment
print re.search(ipAddressRegex,"54.214.20.33")!=None
nameregex=r"(?P<year>\d{4})-(?P<month>\d{2})-(?P<day>\d{2})"
result=re.search(nameregex,"2013-12-08")
print result.group("year")
print result.group("month")
print result.group("day")
print "存在(),捕获分组"
print re.findall('(\w+\.?)+','aaa.bbb.ccc')
print "不存在(),正常捕获"
print re.findall('\w+\.?','aaa.bbb.ccc.ddd')
print "边界:"
print re.findall(r"\b\w+\b","a sentence\tcontains\na lot of words")
string="tomorrow I will wear in brown standing in row 10 next to the rowdy guy"
rowWordRegex=r"\brow\b"
print "提取每行的第一个单词"
string="first line\nsecond line\r\nlast line"
lineBeginWordRegex=r"(?m)^\w+"
print re.findall(lineBeginWordRegex,string)
print "提取整段文本的第一个单词"
string="first line\nsecond line\r\nlast line"
lineBeginWordRegex=r"(?m)\A\w+"
print re.findall(lineBeginWordRegex,string)
print "匹配整段文本的最后一个单词"
stringEndWordRegex=r"\w+$"
string="some sample text"
print re.findall(stringEndWordRegex,string)
string="some sample text\n"
print re.findall(stringEndWordRegex,string)
print "匹配每行的最后一个单词"
string="first line\nsecond line\r\nlast line"
lineEndWordRegex=r"\w+(?m)$"
print re.findall(lineEndWordRegex,string)
print "只使用^和$验证6位数字字符串,可能会有漏洞"
print re.search(r"^\d{6}$","123456")!=None
print re.search(r"^\d{6}$","123456\n")!=None
print "Python不支持\z,它的\Z等价于其他语言中的\z"
print re.search(r"\A\d{6}\Z","123456")!=None
print re.search(r"\A\d{6}\Z","123456\n")!=None
plainText="line1\nline2\nline3"
print plainText
print re.sub(r"(?m)$","</p>",re.sub(r"(?m)^","<p>",plainText))
print "去除行首的空白字符"
whiteSpace=" begin\n between\t\n\nend"
beginSpaceRegex=r"(?m)^\s+"
trimmedLeadingSpace=re.sub(beginSpaceRegex,"",whiteSpace)
print trimmedLeadingSpace
print "去除行尾的空白字符"
endSpaceRegex=r"(?m)\s+$"
trimmedEndingSpace=re.sub(endSpaceRegex,"",trimmedLeadingSpace)
print trimmedEndingSpace
print "多选形式的测试,行不通"
whiteSpace=" begin\n between\t\n\nend"
spaceRegex=r"(?m)(^\s+|\s+$)"
spaceTrimmed=re.sub(spaceRegex,"",whiteSpace)
print spaceTrimmed
print "环视(?=...)肯定顺序环视,向右"
print "环视(?!...)否定顺序环视,向右"
print "环视(?<=...)肯定逆序环视,向左"
print "环视(?<!...)否定逆序环视,向左"
print "去掉中英文混排文本中不必要的空白字符"
mixedString=" 中 英文混排,some English word, 有多余的空 白字符 "
spaceBetweenChineseRegex=r"(?<![a-zA-Z])\s+(?![a-zA-Z])"
print re.sub(spaceBetweenChineseRegex,"",mixedString)
print "["+re.sub(spaceBetweenChineseRegex,"",mixedString)+"]"
negativeSpaceTrimRegex=r"(?<=[^a-zA-Z])\s+(?=[^a-zA-Z])"
print "["+re.sub(negativeSpaceTrimRegex,"",mixedString)+"]"
print "准确匹配主机名的正则表达式"
hostnameRegex=r"^(?=[-a-zA-Z]0-9.]{0,255}(?![-a-zA-Z0-9.]))((?!-)[-a-zA-Z0-9]{1,63}\.)*((?!-)[-a-zA-Z0-9]){1,63}$"
#应该匹配的
re.search(hostnameRegex,"localhost")!=None
re.search(hostnameRegex,"example.com")!=None
#不应该匹配的
#"e"*64 表示将64个"e"连接在一起组成的字符串
re.search(hostnameRegex,"-example.com")!=None
re.search(hostnameRegex,("e"*64)+".com")!=None
re.search(hostnameRegex,"e"*256)!=None
print "十六进制"
print r"[0-9a-fA-F]+"
print "MD5字符串 C37B58C783456BF879C3DD5C519195A2"
print "[0-9a-fA-F]{32}"
print "浮点数 +3.14 +0.07 3.6 -12"
print "(+?([0-9]+|[0-9]+\.[0-9]+|\.[0-9]+)|-?([0-9]+|[0-9]+\.[0-9]+))"
print "逗号分隔的整数 12,414,421 231,457,111 3,456 "
print "(?<![0-9])[0-9]{1,3}(,[0-9]{3})*(?![0-9])"
print "中午字符"
print "[\u4e00-\u9fff]"
print "中文间多余的空白字符"
print "(?<=[\u4e00-\u9fff])\s+(?=[\u4e00-\u9fff])"
print "(?<=[^\x00-\x7f])\s+(?=[^\x00-\x7f])"
print "价格 $ 46 ¥188.88 88.0 12300 "
print "[¥$]?\s*[0-9]+(\.[0-9]{2})?"
#增加断言判断之后不是数字字符
print "[¥$]?\s*[0-9]+(\.[0-9]{2})?(?![0-9]))"
print "手机号码 013801254780 +8613800113366 +86 15212124545 0086 13588887774 "
print "((0|(00|\+)86\s?)?(13[0-9]|15[0-356]|18[025-9])\d{8}"
#增加断言判断之前,之后都不是数字
print "(?<![0-9])((0|(00|\+)86\s?)?(13[0-9]|15[0-356]|18[025-9])\d{8}(?![0-9])"
print "固定电话号码 12345678 021-12345678 0731-12345678 (010)12345678 "
print "(\(?0[1-9]{2,3}\)?-?)?[1-9][0-9]\{6,7}(-[0-9]{1,6})?"
#增加断言判断之前,之后都不是数字
print "(?<![0-9])(\(?0[1-9]{2,3}\)?-?)?[1-9][0-9]\{6,7}(-[0-9]{1,6})?(?![0-9])"
print "邮政编码 100859 412000"
print "[1-9][0-9]{5}"
print "身份证号码 31010019800112071 10010019800101207x 310100800101207 "
print "[1-9][0-9]{14}([0-9]{2}[0-9xX])?"