码迷,mamicode.com
首页 > 编程语言 > 详细

python re 正则表达式汇总

时间:2020-03-27 23:24:48      阅读:82      评论:0      收藏:0      [点我收藏+]

标签:sel   test   http   ext   注释   开始   字号   转义字符   正则   

import re # text = "1a+_1hello\n" # text1 = ‘0731-8888888 abc‘ # ret = re.match(‘he‘,text) # ret = re.match(‘.‘,text) #.表示可以匹配任意字符 # ret = re.match(‘\d‘,text) # 匹配到数字 # ret = re.match(‘\D‘,text) # 匹配的任意非数字和\d相反 # ret = re.match(‘\s‘,text) #匹配空白字符(\n,\t,\r)以上都是开头匹配的状态 # ret = re.match(‘\w‘,text) # 匹配a-z A-Z 数字和下划线 # ret = re.match(‘\W‘,text) # 和\w相反的 # ret = re.match(‘[a1]‘,text) # [] 组合的方式 匹配1 或是 a # ret = re.match(‘[\d\-]+‘,text1) #- 要特殊转义下 才表示- ;[\d\-]表示匹配到一个字符 是数字或是 - 开头的,如果是多个的话用+ # ret = re.match(‘[^0-9]‘,text1) # 匹配到0-9的数字,如果用一个^表示非 # ret = re.match(‘[a-zA-Z0-9_]‘,text1) #这个和\w一样的效果 # ret = re.match(‘[^a-zA-Z0-9_]‘,text1)# 这个是和\W一样 # print(ret.group()) ##########匹配多个字符: # text = "1a+_1hello\n" # text1 = ‘0731-8888888 abc‘ # # ret = re.match(‘\d*‘,text1)# * 表示匹配前面0个或是任意多个字符 # ret = re.match(‘\w+‘,text1) # + 匹配一个或者多个字符,最少一个 按照全面的字符匹配是字符或是数字 # ret = re.match(‘\w?‘,text1) # ?匹配一个或是0个,要么就匹配一个 # ret = re.match(‘\w{2}}‘,text1) # {} 匹配多少个 # ret = re.match(‘\{m,n}+‘,text1) # {m,n}匹配m 到 n个字符 # print(ret.group()) ########## # text = ‘18534523233‘ # text1 = ‘din.zng@qq.com‘ # text2 = ‘http://baidu.com‘ # text3 = ‘42344519900707551x‘ # 要求1开头 2位可以是34587 后面9位随意: # ret = re.match(‘1[34587]\d{9}‘,text) 匹配手机号码 # ret1 = re.match(‘.+@[a-z0-9]+\.[a-z]+‘,text1) # 匹配邮箱 # ret2 = re.match(‘(http|https|ftp)://[^\s]+‘,text2) # print(len(text3)) # ret3 = re.match(‘\d{17}[\dxX]‘,text3) # 匹配shenfenzheng # print(ret3.group()) ################## #脱子豪 ^ 表示以什么开始 # text3 = ‘42344519900707551x‘ # text4 = ‘dfds@163.com‘ # ret3 = re.match(‘^4‘,text3) # 匹配shenfen证,match 函数自带^字号 表示以什么开始 # ret3 = re.search(‘^4‘,text3) # search不自带,所以^表示已4开头的 从什么开始 ## $表示以什么结尾 # ret3 = re.match(‘\w+@163.com$‘,text3) ## 默认情况下 匹配多个字母 这个是贪婪模式 ,非贪婪模式就是后面? # ret3 = re.match(‘\d+?‘,text3) # 结果是 41042319830707541 # ret3 = re.match(‘\d+‘,text3) # 结果是4 安装最小条件匹配 满足条件最短的那个 # text = ‘<h1>标题</h1>‘ # ret3 = re.match(‘<.+>‘, text) ## 结果就是 <h1>标题</h1> 贪婪模式 按照最长的匹配 # ret3 = re.match(‘<.+?>‘, text) ## <h1> 这个是非贪婪模式按照最小的匹配 # print(ret3.group()) # # ret4 = re.match(‘[1-9]\d?$|100$‘, text) # 1-9]\d?只能匹配前面的两位10, 所以匹配不到100, 但是$后10结尾 没有匹配到,然后就匹配了100 #######################转义字符 \ , \\ , \\\\ r##########: # test = ‘dfasdfsdf is $99 dfd‘ # ret = re.search(‘\$\d+‘,test) # \ 转义字符, search 是对整个字符串进行search,但是match是从字符串的第一个开始匹配 # print(ret.group()) # \n 整个是转义字符,如果要去掉转义字符的话 加一个\ , 变成了 \\n # text = r‘\n‘ , r表示raw 这样就表示了 \n # # \ 在正则表达式和python都是做转义的, # text = ‘\c‘ # # python: ‘\\n’ = \n # #\\\\n == \\n # #正则中:\n = # # \\n = \n # ret = re.match(‘\\\\c‘,test) # 所以要写4个,麻烦可以用: # ret = re.match(r‘\\c‘,test) # 在python这一次就不会转义了\\c,但是等到了正则的话 去掉 \ 就变成了 \c 就进行了匹配 # print(ret.group()) #######################re.groups, findall sub, split, compile ############ text = ‘the&apple price is $44 and sell $34‘ # res = re.search(‘.*(\$\d+).*(\$\d+)‘,text) # print(res.group()) # print(res.group(1)) # print(res.groups()) # res = re.findall(‘\$\d+‘,text) # print(res) ## output: [‘$44‘, ‘$34‘] # # res_replace = re.sub(‘\$\d+‘,‘0‘, text) # 函数替换 # print(res_replace) # text = ‘hello world ni hao‘ # res_split = re.split(‘ ‘,text) # res_split = re.split(‘‘,text) # print(res_split) text2 = "the number is 20.50" r = re.compile(‘\d+\.?\d*‘) ret = re.search(r,text2) # compile先放到内存 到时调用 ret = re.compile(r""" #起到一个注释的作用 \d* #小数点前面的数字 \.? # 小数点本身 """,re.VERBOSE) print(ret.group())

python re 正则表达式汇总

标签:sel   test   http   ext   注释   开始   字号   转义字符   正则   

原文地址:https://blog.51cto.com/zhangfang526/2482531

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!