正则表达式检测网站regex101: build, test, and debug regex
#encoding=utf-8
import re
content="苹果是绿色的橙子是橙色的香蕉是黄色的乌鸦是黑色的"
p=re.compile(r'.色')
for one in p.findall(content):
print(one)

"."匹配任意字符(一个)
"*"匹配任意个字符包括0个和任意个
#encoding=utf-8
import re
content=("苹果是,绿色的\n"
"橙子是,橙色的\n"
"香蕉是,黄色的\n"
"乌鸦是,黑色的\n")
p=re.compile(r',.*')
for one in p.findall(content):
print(one)


"+"匹配至少1词

"{6}"重复6次
\d:任意数字
\D:非数字
\s:空字符,如[\t\n\r\f\v]
\S:非空字符[^\t\n\r\f\v]
\w:任意文字字符=[a-zA-Z0-9_]
\W:非文字字符=[^a-zA-Z0-9_]
[12]匹配1或2
[^]表示非
^表示匹配文本的起始位置
单行模式:表示匹配整个文本的开头位置
多行模式:表示匹配文本每行的开头位置
#encoding=utf-8
import re
content="""001-苹果价格-60,
002-橙子价格-70,
003-香蕉价格-80,
"""
import re
p=re.compile(r'^\d+')
print(p.findall(content))

多行模式:表示匹配文本每行的开头位置
python
#encoding=utf-8
import re
content="""001-苹果价格-60,
002-橙子价格-70,
003-香蕉价格-80,
"""
import re
p=re.compile(r'^\d+',re.MULTILINE)
print(p.findall(content))

"$"表示文本的结束位置
如果是单行模式,表示这个文本的结束位置
如果是多行模式,表示文本每行的结束位置
python
#encoding=utf-8
import re
content="""001-苹果价格-60,
002-橙子价格-70,
003-香蕉价格-80,"""
import re
p=re.compile(r'\d+,$',re.MULTILINE)
print(p.findall(content))

"?"取消贪婪模式
匹配模式替换:
#encoding=utf-8
import re
content="""001-苹果价格-60,
002-橙子价格-70,
003-香蕉价格-80,"""
import re
names="关羽;张飞,赵云, 马超,黄忠, 李逵"
p=re.split(r'[;,\s]\s*',names)
print(p)
names='''<a href='https://www.bilibili.com.vieo/av23456543/?p=1'target="_blank">点击这里,这边看视屏</a>
<a href='https://www.bilibili.com.vieo/av23342445/?p=1'target="_blank">点击这里,这边看视屏</a>
<a href='https://www.bilibili.com.vieo/av23486573/?p=1'target="_blank">点击这里,这边看视屏</a>
'''
#替换函数,参数对象是match
def subfun(match):
#Match对象的group(0)返回的是整个匹配上的字符串
src=match.group(0)
#March对象的group(1)返回的是第一个group分组的内容
number=int(match.group(1))+6
dest=(f'/av{number}/')
print(f'{src}替换为{dest}')
return dest
newStr=re.sub(r'/av(\d+?)/',subfun,names)
print(newStr)
