鱼C论坛

 找回密码
 立即注册
查看: 415|回复: 3

问一个正则表达式的问题

[复制链接]
发表于 2018-8-7 16:23:49 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 小小小小的鱼丶 于 2018-8-7 17:10 编辑
content = 'this is my phone number 123456. do you remember'
 s= re.search('(.*?)(\d+).',content)
 s.group(1)
'this is my phone number '
s.group(2)
'123456'

content = 'this is my phone number 123456. do you remember'
re.sub('(.*?)(\d+).','mememem',content)
'mememem do you remember'

content = 'this is my phone number 123456. do you remember'
re.sub('(.*?)(\d+).',r'\1 mememem',content)
'this is my phone number  mememem do you remember'

content = 'help me 1234567 this is a question'
re.sub('(?P<hehe>\d+)',r'(?P=name)<hehe> meme',content)
'help me (?P=name)<hehe> meme this is a question'
这里的反向引用为啥不起作用
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-8-7 19:19:55 | 显示全部楼层
应该是不在一个正则表达式里,识别不出来你想要的hehe组
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-8-8 20:11:53 | 显示全部楼层
新手,不好意思不知道原因。第14行 为什么r'\1 this is my phone number
而r'\2 是 123456
而r'\ 结果是‘\\
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-8-8 21:00:49 | 显示全部楼层
学会一门程序 发表于 2018-8-8 20:11
新手,不好意思不知道原因。第14行 为什么r'\1 this is my phone number
而r'\2 是 123456
而r'\ 结果是 ...

因为r\2 是应引用前面第二个分组的内容 r'\ 说明这是一个原始字符串
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-10-6 05:53

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表