python正则表达式-正则基础

目录

一、任一元素

二、匹配特定的字符类别

         1、\d  \w

三、多个元素

         1、两位元素 [][]

         2、* + ?

         3、重复次数 {}

         4、位置匹配 ^ $

         5、子表达式()


 一、任一元素

        []:1、[ab] 匹配a或b;

       2、[0-9] 匹配任意一个数字;[a-z] 匹配任何一位小写字母;[A-Z] 匹配任何一位大写字母;

       3、[a-zA-Z] 匹配任意一位大小写字母;

       4、. 匹配任意一个字符;^:取反

import re
answer_zimu='aiIJ2db33c4AI'
print('字母:',re.findall('[a-z]',answer_zimu))

answer_shuzi='aiIJ2db33c4AI'
print('数字:',re.findall('[0-9]',answer_shuzi))

answer_str='aiIJ2db33c4AI'
print('大小写:',re.findall('[a-zA-Z]',answer_str))

str1=['sales1.xls','sales2.xls','spac1.xls']
answer_str1=map(lambda x:re.findall('sales.\.xls',x),str1)  #'sales.\.xls'  \转义字符
print('.输出:',list(answer_str1))

str2=['sales1.xls','sales2.xls','salesn.xls']
answer_str2=map(lambda x:re.findall('sales[^0-9]\.xls',x),str2)  #'sales[^0-9]\.xls'  \转义字符
print('取反:',list(answer_str2))

字母: ['a', 'i', 'd', 'b', 'c']
数字: ['2', '3', '3', '4']
大小写: ['a', 'i', 'I', 'J', 'd', 'b', 'c', 'A', 'I']
.输出: [['sales1.xls'], ['sales2.xls'], []]
取反: [[], [], ['salesn.xls']]

二、匹配特定的字符类别

  1、\d  \w

     1、\d 任意一个数字[0-9];\D 任意一个非数字[^0-9];\w 任意一个数字字母下划线[a-zA-Z0-9_];

     2、 \W 任意一个非字数字字母下划线;\s 匹配任意一个空白字符[\f\n\r\t\v];\S 匹配任意一个非空白字符[^\f\n\r\t\v]

import re
str=['12839','A1HY98','9okjcds','siU_U','siU U']
answer_str=map(lambda x:re.findall('\d\d\d\d\d',x),str)
print('数字:',list(answer_str))

answer_str=map(lambda x:re.findall('\w\w\w\w\w\w\w',x),str)
print('六位元素:',list(answer_str))

answer_str=map(lambda x:re.findall('\w\w\w\s\w',x),str)
print('空白字符:',list(answer_str))

数字: [['12839'], [], [], [], []]
六位元素: [[], [], ['9okjcds'], [], []]
空白字符: [[], [], [], [], ['siU U']]

三、多个元素

  1、两位元素 [][]

        [][]:[a-z][0-9] 匹配a、b和c和任意一位数字

import re
str='asc99d71'
print(re.findall('[a-z][0-9]',str))
['c9', 'd7']

   2、* + ?

        * 零个或多个字符;+ 一个或多个字符;? 零个或一个字符           

com=['http://www.ben@forta.com','httpps://www.ben@forta.com','http://www.forta@forta.com']
answer_com1=map(lambda x:re.findall('http[\w]+://[\w.]+@[\w.]+',x),com)
print('+ 输出:',list(answer_com1))

answer_com2=map(lambda x:re.findall('http[\w]*://[\w.]+@[\w.]+',x),com)
print('* 输出:',list(answer_com2))

answer_com3=map(lambda x:re.findall('http[\w]?://[\w.]+@[\w.]+',x),com)
print('? 输出:',list(answer_com3))

+ 输出: [[], ['httpps://www.ben@forta.com'], []]
* 输出: [['http://www.ben@forta.com'], ['httpps://www.ben@forta.com'], ['http://www.forta@forta.com']]
? 输出: [['http://www.ben@forta.com'], [], ['http://www.forta@forta.com']]

  3、重复次数 {}

number=['12345678910','10-6-204','1000-344-0009']
answer_num1=map(lambda x:re.findall('\d{11}',x),number)
print('11位手机号:',list(answer_num1))

answer_num2=map(lambda x:re.findall('\d{0,4}-\d{1}-\d{0,4}',x),number)
print('固定范围:',list(answer_num2))

answer_num3=map(lambda x:re.findall('\d{0,4}-\d{1,}-\d{0,4}',x),number)
print('至少满足:',list(answer_num3)) 

11位手机号: [['12345678910'], [], []]
固定范围: [[], ['10-6-204'], []]
至少满足: [[], ['10-6-204'], ['1000-344-0009']]

   4、位置匹配 ^ $

      ^限制开头   $限制结尾

weizhi=['12345678910','12345678910and','34455667778']
answer_wenzi1=map(lambda x:re.findall('^1\d{10}',x),weizhi)
print('^限制开头:',list(answer_wenzi1))

answer_wenzi2=map(lambda x:re.findall('^1\d{10}$',x),weizhi)
print('^ $限制开头结尾:',list(answer_wenzi2))

answer_wenzi3=map(lambda x:re.findall('\w+[a-z]$',x),weizhi)
print('$限制结尾:',list(answer_wenzi3))

^限制开头: [['12345678910'], ['12345678910'], []]
^ $限制结尾: [['12345678910'], [], []]
^ $限制结尾: [[], ['12345678910and'], []]

 5、子表达式()

       当作一个独立元素来使用,()

str='<b>123</b><b>加粗字体</b><b>斜体</b>'
print('子表达式:',re.findall('<b>(.*?)</b>',str))  #去除两边的模式

子表达式: ['123', '加粗字体', '斜体']
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值