鱼C论坛

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

问一个正则表达式的问题

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

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

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

x
本帖最后由 小小小小的鱼丶 于 2018-8-7 17:10 编辑

  1. content = 'this is my phone number 123456. do you remember'
  2. s= re.search('(.*?)(\d+).',content)
  3. s.group(1)
  4. 'this is my phone number '
  5. s.group(2)
  6. '123456'

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

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

  13. content = 'help me 1234567 this is a question'
  14. re.sub('(?P<hehe>\d+)',r'(?P=name)<hehe> meme',content)
  15. 'help me (?P=name)<hehe> meme this is a question'
  16. 这里的反向引用为啥不起作用

复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2018-8-7 19:19:55 | 显示全部楼层
应该是不在一个正则表达式里,识别不出来你想要的hehe组
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-8-8 20:11:53 | 显示全部楼层
新手,不好意思不知道原因。第14行 为什么r'\1 this is my phone number
而r'\2 是 123456
而r'\ 结果是‘\\
小甲鱼最新课程 -> https://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'\ 说明这是一个原始字符串
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-1-1 07:31

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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