Forty-Pian re module

re module

import re

Regular expressions, also known as regular expressions. Regular expressions are typically used to retrieve, replace the text in line with those of a model (rule) is (that is, the original string that meet the rules took out, not to continue the search)

Metacharacters

s = 'I am king, I amam27 years old, I want to go to study'

# 1. ^ 获取开头字符
res = re.findall('^I',s)
print(res)    # ['I']

# 2. $ 获取结尾字符
res = re.findall('study$',s)
print(res)   # ['study']

# 3. | 左右两边都输出(or),获取左右两边
res = re.findall('I am 27 years|I want to',s)
print(res)    # ['I am 27 years', 'I want to']
res = re.findall('^I|study$',s)
print(res)    # ['I', 'study']

# 4. [] 获取字符串中所有符合[]中的元素,并按顺序输出
 res = re.findall('[oldI]',s)
 print(res)   # ['I', 'I', 'o', 'l', 'd', 'I', 'o', 'o', 'o', 'd']

# 5. . 一个 . 就是获取任意一个字符
res = re.findall('...',s)   每次获取三个任意字符
print(res)    # ['I a', 'm k', 'ing', ', I', ' am', ' 27', ' ye', 'ars', ' ol', 'd, ', 'I w', 'ant', ' to', ' go', ' to', ' st', 'udy']

# 6. {n}就是{}前的一个字符复制出n次之后组成的新字符串,去原字符串中找
# 如果{}前的字符串只有一个字符,且{}中是0,则表示用""(空)去匹配
st = '我我我我我我我我我我,我你,你我我我'
res0 = re.findall('你你{0}',st)   # “你”
res1 = re.findall('我{3}',st)    # “我我我”
res2 = re.findall('你我{3}',st)   # “你我我我”
print(res0)   # ['你', '你']
print(res1)   # ['我我我', '我我我', '我我我', '我我我']
print(res2)   # ['你我我我']

# 7. * 匹配前面的子表达式任意次。*等价于{0,}。
# 例如,zo*能匹配“z”,也能匹配“zo”以及“zoo”、“zooo”.....
st = '我我我,我我,我'
res0 = re.findall('我你*',st)  # “我”、“我你”、“我你你”......
res1 = re.findall('你*',st)   # “”、“你”、“你你”......
print(res0)    # ['我', '我', '我', '我', '我', '我']
print(res1)    # ['', '', '', '', '', '', '', '', '']  似乎与C中的字符串和字符数组有点渊源,最后一个字符可能默认是\n

# 8. +  匹配前面的子表达式一次或多次(大于等于1次)。+等价于{1,}
# 例如,“zo+”能匹配“zo”以及“zoo”,但不能匹配“z”。
st = '我我你我你,我我,我'
print(re.findall('你+',st))   # ['你', '你']
print(re.findall('我你+',st))   # ['我你', '我你']
# 需要理解贪婪和非贪婪,对于这些元字符,不加?都是默认为贪婪,也就是在搜索时,会尽量用匹配前面一个字符多次的去对比原字符串
print(re.findall('我+',st))   # ['我我', '我', '我我', '我']

# 9. ? 匹配前面的子表达式0次或1次.停止符
st = '我我你我你,我我,我,我你他'
print(re.findall('我?',st))  # ['我', '我', '', '我', '', '', '我', '我', '', '我', '', '我', '', '', '']

# 当 ? 字符紧跟在任何一个其他限制符(*,+,?,{n},{n,},{n,m})后面时,匹配模式是非贪婪的。非贪婪模式尽可能少地匹配所搜索的字符串;而默认的贪婪模式则尽可能多地匹配所搜索的字符串。
# 例如,对于字符串“oooo”,“o+”将尽可能多地匹配“o”,得到结果[“oooo”],而“o+?”将尽可能少地匹配“o”,得到结果 ['o', 'o', 'o', 'o']

# 10. \d 匹配数字
stri = 'i am 26 years old'
print(re.findall('\d',stri))   # ['2', '6']

#11. \D  匹配非数字
stri = 'i am 26 years old'
print(re.findall('\D',stri))   # ['i', ' ', 'a', 'm', ' ', ' ', 'y', 'e', 'a', 'r', 's', ' ', 'o', 'l', 'd']

# 12. \w 匹配包括下划线的任何单词字符
st = '我我你_我你,我%我,我,我你他'
print(re.findall('\w',st))  # ['我', '我', '你', '_', '我', '你', '我', '我', '我', '我', '你', '他']
 
# 13 \W 非字母,数字,下划线
print(re.findall('\W',st))   # [',', '%', ',', ',']

# 14 \s 空
print(re.findall('\s',st))  # []

# 15. \S 非空
print(re.findall('\S',st))  # ['我', '我', '你', '_', '我', '你', ',', '我', '%', '我', ',', '我', ',', '我', '你', '他']

res = re.findall('^I.*?study$',s)
print(res)    ['I am king, I am 27 years old, I want to go to study']

# 16. (.*?) 非贪婪模式,找到一个了就停止
# 贪婪模式  .*
s = '孙悟空找猪八戒找媳妇妇妇高$$$$$@@@@翠兰,然____后asdfasdf吃西234234瓜,再吃   西瓜妇'
res = re.findall('猪.*妇',s)
print(res)   # ['猪八戒找媳妇妇妇高$$$$$@@@@翠兰,然____后asdfasdf吃西234234瓜,再吃   西瓜妇']
# 非贪婪模式
res = re.findall('猪.*?妇',s)
print(res)   # ['猪八戒找媳妇']

# 17. re.compile  写一个通用的规则(要用到前面学到的元字符)
s = 'i am 173'
form1 = re.compile('\d')
form2 = re.compile('\D')

res1 = form1.findall(s)
res2 = form2.findall(s)

print(res1)   # ['1', '7', '3']
print(res2)   # ['i', ' ', 'a', 'm', ' ']

# 18. match和search

# re.match从头开始匹配,找一个,如果在开头没有则返回None
s = '从前有座山,山里有个庙'
res = re.match('有',s)
print(res)    # None
s = '有座山,有座庙'
res = re.match('有',s)
print(res)   # <_sre.SRE_Match object; span=(0, 1), match='有'>

# re.search 搜索所有
s = '从前有座山,山里有个庙'
res = re.search('有',s)
print(res)   # <_sre.SRE_Match object; span=(2, 3), match='有'>

# 19. 分组,需要的数据放入()就行了,括号外的就不会打印了
s = '有踢球的,有跑步的,有看书的,'
res = re.findall('有(.*?),',s)
print(res)   # ['踢球的', '跑步的', '看书的']

# 20. re.split  与字符串的相似,只是里面的参数用正则表达式
s = 'i am 26 years old'
res = re.split('\d',s)
print(res)   # ['i am ', '', ' years old']

#21. sub 和 subn

# 和字符串中的replace相似,subn会计算替换的次数
s = '昨天是1号,今天是2号,明天3号,后天4号放假'

# re.sub
sub_res = re.sub('\d','*',s)
print(sub_res)   # 昨天是*号,今天是*号,明天*号,后天*号放假

# re.subn
subn_res = re.subn('\d','*',s)
print(subn_res)   # ('昨天是*号,今天是*号,明天*号,后天*号放假', 4)

Guess you like

Origin www.cnblogs.com/itboy-newking/p/11041723.html