Python 正则表达式中的错误?(re.sub 与 re.MULTILINE) [英] Bug in Python Regex? (re.sub with re.MULTILINE)

查看:45
本文介绍了Python 正则表达式中的错误?(re.sub 与 re.MULTILINE)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到 Python 的 Regex 库中有一些奇怪的行为,我不确定我是否做错了什么.

I'm noticing some odd behavior in Python's Regex library, and I'm not sure if I'm doing something wrong.

如果我使用 re.sub()re.MULTILINE 对其运行正则表达式.它似乎只取代了前几次出现.如果我关闭 re.MULTILINE,使用 re.subn(..., count = 0, flags = re.MULTILINE) 或编译正则表达式,它将替换所有出现的内容使用 re.compile(..., re.MULTILINE).

If I run a regex on it using re.sub(), with re.MULTILINE. It seems to only replace the first few occurrences. It replaces all occurrences if I turn off re.MULTILINE, use re.subn(..., count = 0, flags = re.MULTILINE), or compile the regex using re.compile(..., re.MULTILINE).

我在 Ubuntu 12.04 上运行 Python 2.7.

I am running Python 2.7 on Ubuntu 12.04.

我在以下位置发布了一个随机示例:

I've posted a random example on:

  • Pastebin.com - 终端输出
  • codepad - 脚本,确认行为(re.subn() 除外,在 2.5 中有所不同)
  • Pastebin.com - Output from terminal
  • codepad - Script, confirming behavior (except for re.subn(), which is different on 2.5)

有人可以在他们的机器上确认/否认这种行为吗?

Can someone confirm / deny this behavior on their machine?

意识到我应该继续将其发布到 Python 错误跟踪器上.编辑 2:问题报告:http://bugs.python.org/msg168909

Realized I should go ahead and post this on the Python bug tracker. EDIT 2: Issue reported: http://bugs.python.org/msg168909

推荐答案

使用

re.sub(pattern, replace, text, flags=re.MULTILINE) 

代替

re.sub(pattern, replace, text, re.MULTILINE) 

相当于

re.sub(pattern, replace, text, count=re.MULTILINE)

这是您代码中的错误.

参见 re.sub()

这篇关于Python 正则表达式中的错误?(re.sub 与 re.MULTILINE)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆