classification
Title: re sub subn backreferrence too few replacements
Type: behavior Stage: committed/rejected
Components: Regular Expressions Versions: Python 3.1, Python 2.6
process
Status: closed Resolution: invalid
Dependencies: Superseder:
Assigned To: Nosy List: mrabarnett, muxum
Priority: normal Keywords:

Created on 2011-02-11 19:29 by muxum, last changed 2011-02-12 14:19 by muxum. This issue is now closed.

Messages (3)
msg128422 - (view) Author: (muxum) Date: 2011-02-11 19:29
#download/try this: http://ideone.com/QA6Fg

from re import *
 
s = 'a->b\na->b\nc->b\nc->b\na->d\na->d\ne->b\ne->b\na->f\na->f\ng->b\ng->b\na->h\na->h\ni->b\ni->b\na->j\na->j\nk->b\nk->b\n'
res = subn(r"(.*\n)(\1)+",r"replaced:\1",s,M)
 
print("output: " + res[0])
print("replace count: %d" % res[1])
 
# if it works right the next print wont show
if res[1] == 8: print("WEIRD: too few replacements ("+str(res[1])+")")

#i'd expect this to replace all ocurrences not only the first 8 times
msg128435 - (view) Author: Matthew Barnett (mrabarnett) Date: 2011-02-12 02:52
Argument 4 of re.subn(...) is 'count', the maximum number of replacements to perform, but you're passing in the MULTILINE flag, which happens to have the integer value 8, hence you're limiting the maximum number of replacements to 8.
msg128445 - (view) Author: (muxum) Date: 2011-02-12 14:19
aww ic
History
Date User Action Args
2011-02-12 14:19:32muxumsetnosy: mrabarnett, muxum
messages: + msg128445
2011-02-12 03:00:01r.david.murraysetstatus: open -> closed
nosy: mrabarnett, muxum
resolution: invalid
stage: committed/rejected
2011-02-12 02:52:46mrabarnettsetnosy: mrabarnett, muxum
messages: + msg128435
2011-02-12 02:37:08r.david.murraysetnosy: + mrabarnett
2011-02-11 19:29:07muxumcreate