re正则表达式方法
目录
来源 1、python正则匹配1.1 re.search 正则表达式search将字符串的所有字串尝试与正则表达式匹配,如果所有的字串都没有匹配成功,返回none,否则返回matchobject;(可以理解为“无为0,有为1”) import re s1 = "helloworld,i am 30" w1 = "world" m1 = re.search(w1,s1) if m1: print("find:%s"%m1.group()) 输出为:(m1.group()可以一次输出多个数组--对应字符串,此处匹配到一个world,则输出匹配到的一个字符串) world 1.2 re.match() 正则表达式match只从字符串的开始与正则表达式匹配,匹配成功返回matchobject,否则返回none import re s1 = "helloworld,i am 30" w1 = "world" m1 = re.match(w1,s1) if m1: print("find:%s"%m1.group()) else print("no match") 输出为:(由于re.match()从字符串开头匹配,两个字符串都不匹配,因此无法匹配) no match 1.3 re.match与re.search的区别1.4 检索和替换(1)repl参数是一个函数 (2)re.compile函数 (3)findall
查找字符串中的所有数字: pattern = re.complie(r‘d+‘) #查找数字 result1 = pattern.findall(‘runoob 123 google 456‘) result2 = pattern.findall(‘run88oobgoogle456‘,10) print(result1) print(result2) 输出结果为: [‘123‘,‘456‘] [‘88‘,‘12‘] result2 = pattern.findall(‘run88oobgoogle456‘,10)中的0指的是字符串中的起始位置,10指的是字符串的结束位置,默认为字符串的长度; (4)re.finditer (5)re.split 1.5 正则表达式对象(1)re.RegexObject (2)re.MatchObject 1.6 正则表达式修饰符 - 可选标志1.7 正则表达式模式1.8 正则表达式实例1.9 正则表达式re.findall用法语法: (1)一般情况 regular_v1 = re.findall(r"docs","https://docs.python.org/3/whatsnew/3.6.html") print(regual_v1) 输出结果为: (2)符号^表示匹配https开头的字符串返回 regular_v2 = re.findall(r"^https","https://docs.python.org/3/whatsnew/3.6.html") print(regular_v2) 输出结果为: (3)用$符号表示以html结尾的字符串返回,判断是否字符串结束的字符串 regular_v3 = re.findall(r"html$","https://docs.python.org/3/whatsnew/3.6.html") print(regular_v3) 输出结果为: (4)[...]匹配括号中的其中一个字符 regular_v4 = re.findall(r"[t,w]h","https://docs.python.org/3/whatsnew/3.6.html") print(regular_v4) 输出结果为: (5)“d”是正则语法规则用来匹配0到9之间的数返回列表 regular_v5 = re.findall(r"d","https://docs.python.org/3/whatsnew/3.6.html") regular_v6 = re.findall(r"ddd","https://docs.python.org/3/whatsnew/3.6.html/1234") print(regular_v5) print(regular_v6) 输出结果为: [‘3‘,‘3‘,‘6‘] [‘123‘] (6)小d表示取数字0-9,大D表示不要数字,也就是除了数字以外的内容返回 regular_v7 = re.findall(r"D","https://docs.python.org/3/whatsnew/3.6.html") print(regular_v7) 输出结果为: (7)“w”在正则里面代表匹配从小写a到z,大写A到Z,数字0到9 regular_v8 = re.findall(r"w","https://docs.python.org/3/whatsnew/3.6.html") print(regular_v8) 输出结果为: (8)“W”在正则里面代表匹配除了字母与数字以外的特殊符号 regular_v8 = re.findall(r"W","https://docs.python.org/3/whatsnew/3.6.html") print(regular_v8) 输出结果为: (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |