re2

import re
# s="""bottle\nbag\nbig\napple"""
# for i ,c in enumerate(s,1):
# print((i-1,c),end='\n' if i % 7 ==0 else '\t')
# print()
# m=re.match('a',s)
# m=re.search('b',s)
#单次
#match 默认必须从0处匹配上,或指定位置开始匹配
#searh 在字符串中搜索,从0或者指定位置开始向后搜索
#fullmatch 全长匹配,一个不落(如果指定子串,子串要全匹配)
# print(type(m),m)

# regex=re.compile('b',re.M) #如果处理多行数据,提前编译节省时间
# m=regex.search(s)
# print(m)
# m=re.fullmatch('b.*',s,re.M|re.S)
# print(type(m),m)


#match、search函数可以返回match对象;findall返回字符串列表;finditer返回一个个match对象
#如果pattern中使用了分组,如果有匹配的结果,会在match对象中
#


#全文
#findall 在文本中,全文搜索,匹配多次,返回列表,元素是字符串
#如果有分组,1组返回分组1的字符串,多组返回字符串元组,元组中元素一定是组匹配的内容
#m=re.findall('b(\w+)',s)
# print(type(m),m)
# for i in m:
# print(type(i),i)
# <class 'str'> ottle
# <class 'str'> ag
# <class 'str'> ig
# #-----------
# m=re.findall('(b)(\w+)',s)
# print(type(m),m)
# for i in m:
# print(type(i),i)
# <class 'tuple'> ('b', 'ottle')
# <class 'tuple'> ('b', 'ag')
# <class 'tuple'> ('b', 'ig')
#finditer 全文搜索,但是返回迭代器,元素是Match对象
# #m=re.finditer('(b\w+)\s(b\w+)\s(b\w+)',s)
# for i in m:
# print(i,i.groups(),i.group(1))
# <re.Match object; span=(0, 14), match='bottle\nbag\nbig'> ('bottle', 'bag', 'big') bottle


# m=re.findall('b\w+',s)
# print(type(m),m)
# m=re.finditer('b\w+',s)
# print(type(m),m)
# for i in m:
# print(type(i),i,i[0])
#替换方法:sub subn
# x=re.sub('b\w','MMM',s,1)
# print(type(x),x)

#分组
# m=re.search('(b)(\w+)',s)
# print(type(m),m[0],m.groups(),m.group(1),m.group(0))
# print(m)
#
# m=re.findall('b(\w+)',s)
# print(type(m),m)
# for i in m:
# print(type(i),i)


# m=re.finditer('(b\w+)\s(b\w+)\s(b\w+)',s)
# for i in m:
# print(i,i.groups(),i.group(1))
# x=re.match('(b\w+)\s(?P<name2>b\w+)\s(?P<name3>b\w+)',s)
# print(x.group())
# print(x.groupdict())
# print(x[1],x.group(2),x.group(3))
# print(x.group('name3'),x['name2'])


#
# 字符串的分割函数split,太难用,不能指定多个字符进行分割。
# re.split(pattern, string, maxsplit=0, flags=0)
# re.split分割字符串
# import re
# s = """
# os.path.abspath(path)
# normpath(join(os.getcwd(), path)).
# """
# # 把每行单词提取出来
# print(s.split()) # 做不到['os.path.abspath(path)',
# 'normpath(join(os.getcwd(),', 'path)).'
# print(re.split('[\.()\s,]+', s))
# print(re.split('[.(),\s]+',s))# +是对前面的模式匹配
# x=re.split('[.(),\s]+',s) #遍历一下,去掉空格,取出想要的单词
# for i in x:
# if i !='':
# print(i,end=' ')
# #
# print(*filter(None,re.split('[.(),\s]+',s)))#返回一个迭代器,
#<filter object at 0x0000024E36828070>
#os path abspath path normpath join os getcwd path

# s1='xxxsasjk'
# obj=iter(s1)
# print(*obj)#* 是对迭代器取值
# x x x s a s j k


s='a\t\t\tb\t\t\nc'
print(s.split())# \s+ 默认空白字符加,尽可能多的切
#['a', 'b', 'c']

posted @ 2022-09-19 11:20  红丿领巾  阅读(167)  评论(0)    收藏  举报