# -*- config=utf-8 -*-#正则表达式#1、处理文本和数据#2、是对字符串操作的一种逻辑公式import re;#Python通过re模块提供对正则表达式的支持。使用re的一般步骤是先将正则表达式的字符串形式编译为Pattern实例,# 然后使用Pattern实例处理文本并获得匹配结果(一个Match实例),最后使用Match实例获得信息,进行其他的操作。pattern=re.compile(r"hello");
match=pattern.match("hello Word");if match: print(match.group());#================== .匹配任意字符(除了\n)===========================ma=re.match(r"a","a");#只想匹配aprint(ma.group());#返回 a 如果匹配失败则为空ma1=re.match(r".","d");print(ma1.group());# 返回 dma2=re.match(r"{.}","{c}");# 匹配大括号中任意字符(除了\n)print(ma2.group());#=================== [...] 匹配任意字符集===================ma3=re.match(r"{[abc]}","{a}");#匹配大括号abc中任意字符print(ma3.group());
ma4=re.match(r"[a-z]","b");#匹配a-z 小写任意字符print(ma4.group());
ma5=re.match(r"[a-zA-Z]","F");#匹配 a-z 任意a字符print(ma5.group());
ma6=re.match(r"[a-zA-Z0-9]","8");#匹配任意字母与数字print(ma6.group());#================== \w 匹配任意单词字符 =================ma7=re.match(r"[\w]","4"); #print(ma7.group());#=====================\W 任意非单词字符 =========================ma8=re.match(r"[\W]","*");print(ma8.group());#===================\d 匹配数字===================ma9=re.match(r"[\d]","3"); #等于[0-9]print(ma9.group());#==================== \D 匹配非数字========================ma9=re.match(r"[\D]","(");print(ma9.group());#======================\s 匹配空格=======================ma10=re.match(r"[\s]"," ");print(ma10.group());#======================\S 匹配非空格=======================ma11=re.match(r"[\S]","撒");print(ma11.group());#========================= \ 转义=========================ma12=re.match(r"\[[\w]\]","[2]");#匹配中括号中任意字符print(ma12.group())#=============================# -*- config=utf-8 -*-import re;#=================* 匹陪前一个字符 0次或无限次==========ma=re.match(r"[A-Z][a-z]","Aa");print(ma.group());#Aama1=re.match(r"[A-Z][a-z]*","Fdsdasd22");#[a-z]无限多个print(ma1.group());#Fdsdasd#================== + 匹配前一个字符一次或无限次============ma2=re.match(r"[_a-zA-Z]+[_\w]*","_dasd");#匹配 _ 或者字母开头的任意字符print(ma2.group());#=================== ?匹配前一个字符0次或1次=========================ma3=re.match(r"[1-9]?[0-9]","10");#匹配一个正两位数包括0print(ma3.group());
ma4=re.match(r"[1-9]?[0-9]","08");#print(ma4.group());#0#====================== {m}匹配前一个字符m次 ========================ma5=re.match(r"[0-9]{6}","1313123");#匹配0-9任意字符6次print(ma5.group());#131312#====================== {m,n}匹配前一个字符m-n次 ====================ma6=re.match(r"[a-zA-Z0-9]{3,20}@163.com","jalja365@163.com");#匹配163邮箱print(ma6.group());#=============== *? +? ??尽可能的少匹配===================ma7=re.match(r"[0-9][a-z]*","2we");print(ma7.group());#2wema8=re.match(r"[0-9][a-z]*?","2we");print(ma8.group());#2ma9=re.match(r"[0-9][a-z]+?","2we");print(ma9.group());#2wma10=re.match(r"[0-9][a-z]??","2we");print(ma10.group());#2w#========================================# -*- config=utf-8 -*-import re;#===================== search(pattern,String,flags=0)在一个字符串中查找匹配 ===================str1="jalja_365—1321";
ma=re.search(r"\d+",str1);#获取字符串中第一次出现的数字print(ma.group());#365#=================findall(pattern,String,flags=0)返回所有匹配部分的列表===================str2="java=90,python=99,c==300";
ma2=re.findall(r"\d+",str2);print(ma2);# ['90', '99', '300'] 获取所有的数字 以列表的形式返回num=sum([int(x) for x in ma2]);#求列表所有元素的和#==================sub()将字符串中匹配正则的字符替换成新的字符串=====================str3="java=99";
ma3=re.sub(r"\d+","100",str3);print(ma3);#java=100#使用函数def add_1(match):#match 是macth对象即sub()的第一个参数
val=match.group(); print(val); return str(int(val)+1);
ma4=re.sub(r"\d+",add_1,str3);print(ma4);#java=100#==============split()根据匹配规则分割字符串返回列表====================str4="jalja:c c++ java Python js,c#";
ma5=re.split(r":| |,",str4);print(ma5);#['jalja', 'c', 'c++', 'java', 'Python', 'js', 'c#'] |