码迷,mamicode.com
首页 > 其他好文 > 详细

复习re

时间:2020-11-11 16:40:49      阅读:6      评论:0      收藏:0      [点我收藏+]

标签:匹配   adf   dfa   idfa   name   练习   dsfa   什么什么   www   

#__author: "ZXYang"
#date: 2020/4/23


import re
from re import findall

# ret = re.findall(‘w..l‘, ‘hello world‘)
# print(ret)

# ^
# ret = re.findall(‘^w..l‘, ‘hello world‘)
# ret = re.findall(‘^h..l‘, ‘hello world‘)

# $
# ret = re.findall(‘w..l$‘, ‘hello world‘)
# ret = re.findall(‘w..l$‘, ‘hello worl‘)

# ret = re.findall(‘[^ab]‘, ‘absf‘)


# *
# ret = re.findall(‘ab*‘, ‘hello worldba‘)
# ret = re.findall(‘a.*x‘, ‘adfsadfasdfsdfx‘)

# +
# ret = re.findall(‘c+d‘, ‘hello bbbbccccdworlcd‘)
# print(ret)

# ? [0, 1]
# ret = re.findall(‘c?d‘, ‘hello cccdworlcdsdvvd‘)

# {}
# ret = re.findall(‘a{1,3}b‘, ‘aaaab‘)
# print(ret)

# ret = re.findall(‘a?‘, ‘abhello world‘)

# []
# ret = re.findall(‘a[c,d]e‘, ‘ade‘)
# ret = re.findall(‘[a-zA-Z0-9]‘, ‘asAD12‘)
# ret = re.findall(‘[^ab]‘, ‘absfasdfasdf‘)
# ret = re.findall(‘[^1,2]‘, ‘asfd1234‘)

# \
# \d
# print(re.findall(‘\d{5}‘,‘fadsfa12345sf543‘))
# \s
# print(re.findall(‘\sad‘,‘f adsf ad12345sf543‘))
# print(re.findall(‘\SadC‘,‘f adsfadC12345sf543‘))
# \W
# print(re.findall(‘\w‘,‘f adf a43‘))
# print(re.findall(‘\wad‘,‘fad sfad43‘))

# \b --以什么什么特殊字符为边界(空格/逗号/特殊符号)
# print(re.findall(r‘I\b‘, ‘fI adI,faI$43‘))
# print(re.findall(r‘I\b‘, ‘fadI sI,dafI$a ‘))
# print(re.findall(r‘\bI‘, ‘Idfa Ifa, IFASDIF‘))
# print(re.findall(r‘\BI‘, ‘fa,FASDIF‘))

# search
# print(re.search(‘sb‘, ‘sfeesbff‘))
# print(re.search(‘sb‘, ‘sbfee sbff‘))

# group
# print(re.search(‘sb‘, ‘sbfee sbff‘).group())
# print(re.search(‘sb‘, ‘absdfdaasb‘).group())
# print(re.search(‘a.‘, ‘asf‘).group())
# print(re.search(‘a\+‘, ‘a+sf‘).group())


# r ==》 代表原生字符串 -------

# print(re.findall(r‘\\p‘, ‘fadf\p‘))
# print(re.findall(‘\\\\‘, ‘fadf\\p‘))

# print(re.findall(r‘\bbl‘, ‘blow‘))

# ()+
# print(re.search(‘ab+‘, ‘assababbb‘).group())
# print(re.search(‘(ab)‘, ‘assabab‘).group())
# print(re.search(‘(ab)+‘, ‘assabab‘).group())

# |
# print(re.search(‘(as)|2‘, ‘as‘).group())
# print(re.search(‘as|2‘, ‘2assabab‘).group())



# match()
# print(re.match(‘aa‘, ‘sdfaa‘))
# print(re.match(‘aa‘, ‘aasdfaa‘).group())


# split
# print(re.split(‘[js]‘, ‘ajdsbf‘))

# sub subn(可查看替换次数)
# print(re.subn(‘z..y‘, ‘aaa‘, ‘dafafsbzxsyfzxxyd‘, 2))
# print(re.sub(‘a..m‘,‘zxyang‘, ‘fafaaaxxmaa‘))

# compile
# obj = re.compile(‘\.com‘)
# print(obj.split(‘fasdf.comcc‘))
# print(obj.findall(‘fasdfasd.comddd‘))


# 练习
# print(re.search(‘(?P<name>\d{3})‘, ‘adf123fd55‘).group())
# obj = re.compile(‘\d{3}‘)
# ret = obj.search(‘ads123‘).group()
# print(ret)

# a = re.finditer(‘\d‘, ‘faaa123fd‘)
# print(next(a))
# print(next(a))
# print(next(a).group())


# ?: 可以取消优先级
# print(re.findall(‘www.(?:\w+).com‘, ‘www.baidu.com‘))

# * [o, +oo] + [1, +oo] ?[0,1]
# {0,+} {1,+oo} {0,1}

# ?P
# print(re.search(‘(?P<id>\d{3})/(?P<name>\w{3})‘, ‘saaa123/kkkaa‘).group())
# print(re.search(‘(?P<id>\d{3})/(?P<name>\w{4})‘, ‘112233/aabbcc‘).group())

# 总结:
# findall():将所有结果返回到一个列表
# search():返回到的第一个对象,group()方法可以返回结果
# match():只在字符串开始位置匹配,也返回匹配到第一个对象

复习re

标签:匹配   adf   dfa   idfa   name   练习   dsfa   什么什么   www   

原文地址:https://www.cnblogs.com/zxy01/p/13767746.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!