----- basis reptiles regular expression (pattern modifier metacharacters and presentation)

1. Yuan character type

# 元字符
'''
. 除换行外任意一个字符
^ 匹配开始位置
$ 结束位置
* 0\1\多次
?0\1次
+ 1\多次
{n} 恰好出现n次
{n,} 至少n次
{n,m} 至少n次,至多m次
| 模式选择符或
() 模式单元
'''

1.2. Case Code :( operating results have been marked, copy the code specific results of their own tests)

string = "taoyunnnnj i51468525aoyubaidu"
# 输出taoyun
data = "tao..."
# 输出taoyun 如果是"^ao.."报错报错---因为字符串不是a开始的
data = "^tao.."
# 输出baidu
data = "ba..$"
# 全部输出从a开始
data = "ao.*"
# 全部输出从t开始
data = "tao.+"
# 多个n全部输出
data = "taoyun+"
# n输出3个,想要输出几个大括号写多少就OK!
# 如果n>已有n的数量----报错报错
data = "taoyun{3}"
# 至少输出3个,加逗号即可
data = "taoyun{3,}"
ret =re.search(data,string)
print(ret)

2. Pattern Modifiers type:

# 模式修正符 *代表长用的
'''
I 忽略大小写*
M 多行匹配*
L 本地化识别匹配(少用)
U Unicode编码
S 让.(点)匹配包括换行*

'''

2.1 Case Code :( operating results have been marked, copy the code specific results of their own tests)

string = "python"
# 正常输出pyt,如果data = "Pyt"运行结果则为None
data = "Pyt"
# 为了解决大小写问题,我们要在函数里面加如下
result = re.search(data,string,re.I)
print(result)

 

Published 98 original articles · won praise 34 · views 30000 +

Guess you like

Origin blog.csdn.net/weixin_42133768/article/details/96485165