阅读背景:

python 极客学院 正则表达式

来源:互联网 
!-- flowchart 箭头图标 勿删 --
import re
from re import findall,search,sub,S

searet_code = 'hadfalifexxIxxfasdjifja134xxlovexx23345sdfxxyouxx8dfse'

# .的使用 相当于一个占位符
# a = 'xy123'
# b = re.findall('x..',a)
# print(b)

# *的使用 匹配无限次 如果不是单一的一个字符,则不会出现空的
# a = 'xyxy123'
# b = re.findall('x*',a)
# print(b)

# ?的使用
# a = 'xxxxxxy123'
# b = re.findall('x?',a)
# print(b)

# 上面的了解就可以啦,需要掌握的只有一个  (.*?)

# .*的使用
# c = re.findall('xx.*xx',searet_code)
# print(c)

# .*?的使用
# c = re.findall('xx.*?xx',searet_code)
# print(c)

# (.*?)的使用
# c = re.findall('xx(.*?)xx',searet_code)
# print(c)

# s = '''sdfxxhello
# xxfsdfxxworldxxasdf'''
#
# d = re.findall('xx(.*?)xx',s,re.S)   # re.S .表示包括了换行符
# print(d)

# 对比findall与search的区别
# s2 = 'asdxxIxx123xxlovexxdfd'
# f = re.search('xx(.*?)xx123xx(.*?)xx',s2).group(1)
# print(f)
#
# f2 = re.findall('xx(.*?)xx123xx(.*?)xx',s2)
# print(f2[0][1])

# sub的使用举例
# s = '123sssss123'
# output = re.sub('123(.*?)123','123%d123'%789,s)
# print(output)

# 演示不同的导入方法   不推荐这样使用from re import findall,search,sub,S
# info = findall('xx(.*?)xx',searet_code,S)
# for each in info:
#     print(each)

# 不需使用compile 多此一举(程序自己会编译)
# pattern = 'xx(.*?)xx'
# new_pattern = re.compile(pattern,re.S)
# output = re.findall(new_pattern,searet_code)
# print(output)

# 匹配纯数字
# a = 'asdfasdf1234567fas888dfas'
# b = re.findall('(\d+)',a)
# print(b)
import re



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: