这所谓的特殊模式很难用正则表达式搜索出来。不过可以先用正则搜索出四个字的词语,然后再进行判断
xxyy模式:r[0]==r[1] and r[2]==r[3]
sums = ['一', '二', '三', '四']
数字模式:[True if i in sums else False for i in r] # 判断词语是否有数字
animal = ['猫', '狗', '鸡', '鸭']
动物模式: [True if i in animal else False for i in r] # 判断词语是否有动物
python正则表达式是:
'hing'
'\wing'
'123456'
'\d\d\d\d\d\d'
'regexpy'
'\py'
正则表达式(简称为 regex)是一些由字符和特殊符号组成的字符串, 描述了模式的重复或者表述多个字符。正则表达式能按照某种模式匹配一系列有相似特征的字符串。换句话说, 它们能够匹配多个字符串。
孤立的一个正则表达式并不能起到匹配字符串的作用,要让其能够匹配目标字符,需要创建一个正则表达式对象。通常向compile()函数传入一个原始字符形式的正则表达式,即 r''。
要让正则表达式不区分大小写,可以向 recompile()传入 reIGNORECASE 或 reI,作为第二个参数。通过传入 reDOTALL 作为 recompile()的第二个参数,可以让句点字符匹配所有字符,包括换行字符。
匹配以某个字符串开头,以某个字符串结尾的情况的正则表达式:^abcqwe$
Python正则表达式的几种匹配用法:
1测试正则表达式是否匹配字符串的全部或部分
regex=ur"" #正则表达式
if research(regex, subject):
do_something()
else:
do_anotherthing()
2测试正则表达式是否匹配整个字符串
regex=ur"/Z" #正则表达式末尾以/Z结束
if rematch(regex, subject):
do_something()
else:
do_anotherthing()
3创建一个匹配对象,然后通过该对象获得匹配细节(Create an object with details about how the regex matches (part of) a string)
regex=ur"" #正则表达式
match = research(regex, subject)
if match:
# match start: matchstart()
# match end (exclusive): atchend()
# matched text: matchgroup()
do_something()
else:
do_anotherthing()
4获取正则表达式所匹配的子串(Get the part of a string matched by the regex)
regex=ur"" #正则表达式
match = research(regex, subject)
if match:
result = matchgroup()
else:
result = ""
5 获取捕获组所匹配的子串(Get the part of a string matched by a capturing group)
regex=ur"" #正则表达式
match = research(regex, subject)
if match:
result = matchgroup(1)
else:
result = ""
6 获取有名组所匹配的子串(Get the part of a string matched by a named group)
regex=ur"" #正则表达式
match = research(regex, subject)
if match:
result = matchgroup"groupname")
else:
result = ""
7 将字符串中所有匹配的子串放入数组中(Get an array of all regex matches in a string)
result = refindall(regex, subject)
8遍历所有匹配的子串(Iterate over all matches in a string)
for match in refinditer(r"<()/s//1>", subject)
# match start: matchstart()
# match end (exclusive): atchend()
# matched text: matchgroup()
9通过正则表达式字符串创建一个正则表达式对象(Create an object to use the same regex for many operations)
reobj = recompile(regex)
10用法1的正则表达式对象版本(use regex object for if/else branch whether (part of) a string can be matched)
reobj = recompile(regex)
if reobjsearch(subject):
do_something()
else:
do_anotherthing()
11用法2的正则表达式对象版本(use regex object for if/else branch whether a string can be matched entirely)
reobj = recompile(r"/Z") #正则表达式末尾以/Z 结束
if reobjmatch(subject):
do_something()
else:
do_anotherthing()
12创建一个正则表达式对象,然后通过该对象获得匹配细节(Create an object with details about how the regex object matches (part of) a string)
reobj = recompile(regex)
match = reobjsearch(subject)
if match:
# match start: matchstart()
# match end (exclusive): atchend()
# matched text: matchgroup()
do_something()
else:
do_anotherthing()
13用正则表达式对象获取匹配子串(Use regex object to get the part of a string matched by the regex)
reobj = recompile(regex)
match = reobjsearch(subject)
if match:
result = matchgroup()
else:
result = ""
14用正则表达式对象获取捕获组所匹配的子串(Use regex object to get the part of a string matched by a capturing group)
reobj = recompile(regex)
match = reobjsearch(subject)
if match:
result = matchgroup(1)
else:
result = ""
15用正则表达式对象获取有名组所匹配的子串(Use regex object to get the part of a string matched by a named group)
reobj = recompile(regex)
match = reobjsearch(subject)
if match:
result = matchgroup("groupname")
else:
result = ""
16用正则表达式对象获取所有匹配子串并放入数组(Use regex object to get an array of all regex matches in a string)
reobj = recompile(regex)
result = reobjfindall(subject)
17通过正则表达式对象遍历所有匹配子串(Use regex object to iterate over all matches in a string)
reobj = recompile(regex)
for match in reobjfinditer(subject):
# match start: matchstart()
# match end (exclusive): matchend()
# matched text: matchgroup()
欢迎分享,转载请注明来源:浪漫分享网
评论列表(0条)