# coding=utf-8 ################ # re 正则表达式 # ################ import re """ 正则表达式元字符 """ s = r"abc" print re.findall(s, "assffsdfdsfsfsd") print re.findall(s, "abcfrefsafabcsfaabc") # [],取其中的任何一个字符 str = "top tip tqp twp tep" res = r"top" print re.findall(res, str) res = r"t[io]p" str = "top tip tqp twp tep" print re.findall(res, str) # 取字符集之外的所有字符,注意,与单独使用^不同 res = r"t[^io]p" str = "top tip tqp twp tep" print re.findall(res, str) # 去[io$]中的任意字符,注意,$在这代表的是普通字符,不是最后元字符 res = r"t[io$]p" str = "top tip tqp twp tep" print re.findall(res, str) print "#"*20 # ^ 匹配字符串的开头 # 匹配开头是hello res = r"^hello" print re.findall(res, str) # 行首没有hello,匹配为空 str = "word, hello boy" print re.findall(res, str) print "#"*20 # $ 匹配字符的结尾 res = r"boy" str ="hello word,hello boy" print re.findall(res, str) res = r"boy$" str ="hello word,hello boy" print re.findall(res, str) res = r"boy$" str ="hello word,hello boys" print re.findall(res, str) print "#"*20 # 正则字符当正常字符使用,使用转义 res = "^abc" str = "^abc ^abc ^abc" print re.findall(res, str) # 转义元字符 res = "\^abc" str = "^abc ^abc ^abc" print re.findall(res, str) # 正则集合,数字 res = r'[0-9]' str = "123456sre789sf0" print re.findall(res, str) res = r'[\d]' str = "123456sre789sf0" print re.findall(res, str) # 取反 res = r'[\D]' str = "123456sre789sf0" print re.findall(res, str) # 正则集合,字母、数字和_ res = r'[A-Za-z0-9_]' str = "123456sre789_sf0AD@¥%………………%" print re.findall(res, str) res = r'[\w]' str = "123456sre_789sf0AD@¥%………………%" print re.findall(res, str) # 取反 res = r'[\W]' str = "123456sre7_89sf0AD@%%" print re.findall(res, str) # *(将*前面的字符重复0次或多次) res = r"ab*" str = "a b ab abc " print re.findall(res, str) # +(+前面的字符匹配1次或多次) res = r"ab+" str = "a b ab abc " print re.findall(res, str) # ? 可有可无,前面的字符重复0次或1次 res = r"^010-*\d{8}" str = "010-12345678" print re.findall(res, str) res = r"^010-?\d{8}" str = "010-12345678" print re.findall(res, str) # 贪婪模式,加载重复后面 res = r"ab+" str = "abbbbbbbbbbb" print re.findall(res, str) # 非贪婪模式,取最小重复值 res = r"ab+?" str = "abbbbbbbbbbb" print re.findall(res, str) # {}重复 res = r"^010-\d\d\d\d\d\d\d\d$" str = "010-12345678" print re.findall(res, str) res = r"^010-\d{8}$" str = "010-12345678" print re.findall(res, str) # {m,n}至少重复m次,最多重复n次 res = r"ab{2,6}" str = "abbbbbbbbbbb" print re.findall(res, str) res = r"ab{15,19}" str = "abbbbbbbbbbb" print re.findall(res, str) """ 正则表达式函数 """ # 匹配电话号码 res = r"\d{3,4}-?\d{7,8}" str ="010-12345678 01087654321 0531-33355566 0621123456" print re.findall(res, str) # compile 正则编译 res = r"\d{3,4}-?\d{7,8}" p_tel = re.compile(res) str ="010-12345678 01087654321 0531-33355566 0621-123456" print p_tel.findall(str) # match() 字符串中找正则对应的字符,匹配开头为正则的 # 如果能匹配,返回的不是字符串,group查看结果 # 如果没有匹配,返回None res = "abc" str = "abc bca" x = re.match(res, str) print x print x.group() res = "abc" str = "babc bca" y = re.match(res, str) print y print "*"*30 # search() 查找字符串中匹配的正则,知道找到一个为止,而不是找到所有 res = "abc" str = "bca abc" print re.search(res, str) res = "abc" str = "abc bca abc" z = re.search(res, str) print z print z.group() # finditer 返回迭代器 res = "abc" str = "babc bca abc" a = re.finditer(res, str) print a # sub 正则替换 res = "java" str = "hello java.This java program?" replaceStr = "python" print re.sub(res, replaceStr, str) # split 切割 ip = "192.168.1.1" print ip.split(".") res = r"[\+\-\*]" str ="1+2-3*4" print re.split(res, str) """ 正则表达式修饰符--可选标志 """ # re.I 正则不区分大小写 res = re.compile(r"abc", re.I) str = "ABC ABC abc AbC" print res.findall(str) # re.S 是匹配包括换行在内的字符 res = r"baidu.com" str = "www.baidu.com" print re.findall(res, str) res = r"baidu.com" str = "www.baidu\ncom" print re.findall(res, str) res = r"baidu.com" str = "www.baidu\ncom" print re.findall(res, str, re.S) # re.M 多行获取匹配 res = r"python" str = """ hello python python hello java hello c python hello perl """ print re.findall(res, str) print re.findall(res, str, re.M) # re.X 正则取消换行 res = r""" \d{3,4} -? \d{8} """ str = "010-12345678 051287654321" print re.findall(res, str) print re.findall(res, str, re.X) """ 分组(|) """ res = r"\w{3}@(\.com|\.cn)" print "*"*50
07-06
07-06
07-07
07-07
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交