目录
一、任一元素
二、匹配特定的字符类别
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、位置匹配 ^ $
^限制开头 $限制结尾文章来源:https://www.toymoban.com/news/detail-449877.html
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、子表达式()
当作一个独立元素来使用,()文章来源地址https://www.toymoban.com/news/detail-449877.html
str='<b>123</b><b>加粗字体</b><b>斜体</b>'
print('子表达式:',re.findall('<b>(.*?)</b>',str)) #去除两边的模式
子表达式: ['123', '加粗字体', '斜体']
到了这里,关于python正则表达式-正则基础的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!