Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
332 views
in Technique[技术] by (71.8m points)

Bug in Python Regex? (re.sub with re.MULTILINE)

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

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).

I am running Python 2.7 on Ubuntu 12.04.

I've posted a random example on:

  • 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?

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Use

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

instead of

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

which is equivalent to

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

which is a bug in your code.

See re.sub()


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...