正则表达式的使用举例re

#encoding=utf-8

# 密码:hadkfalifexxlxxfasdjifja134xxlovexx23345sdfxxyouxx8dfse
# 答案:I love you

#正则表达式
import re
secret_code = 'hadkfalifexxlxxfasdjifja134xxlovexx23345sdfxxyouxx8dfse'

#.的使用举例
# a = 'xz123'
# b = re.findall('x..',a)
# print b

#*的使用举例
# a = 'xyxy123x'
# b = re.findall('x*',a)
# print b

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

# #.*的使用举例
# b = re.findall('xx.*xx',secret_code)
# print b
#
# #.*?的使用举例
# c = re.findall('xx.*?xx',secret_code)
# print c
#
# #()的使用举例
# d = re.findall('xx(.*?)xx',secret_code)
# print d
# for each in d:
#     print each,

# s = '''sdfxxhello
# xxfsdfxxworldxxasdf'''
# #re.S使.匹配包括换行在内的所有字符
# d = re.findall('xx(.*?)xx',s,re.S)
# print d

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

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

# #匹配数字
# a = 'asdfasf1234567fasd555fas'
# b = re.findall('(\d+)',a)
# print b
#
# a = 'asdfasf1234567fasd555fas'
# b = re.findall('asf(.*?)fasd(.*?)fas',a)
# print b
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值