classification
Title: re.sub() replaces only several matches
Type: Stage: resolved
Components: Regular Expressions Versions: Python 3.1
process
Status: closed Resolution: duplicate
Dependencies: Superseder: re.sub confusion between count and flags args
View: 11957
Assigned To: Nosy List: arysin, ezio.melotti, mrabarnett
Priority: normal Keywords:

Created on 2011-05-14 15:04 by arysin, last changed 2011-05-14 22:13 by arysin. This issue is now closed.

Files
File name Uploaded Description Edit
repl.sh arysin, 2011-05-14 15:04 sed replace which works
repl.py arysin, 2011-05-14 15:05 Python replace which does not work
test.txt arysin, 2011-05-14 15:05 Text to replace
Messages (4)
msg135975 - (view) Author: Andriy Rysin (arysin) Date: 2011-05-14 15:07
When trying to replace strings with parenthesis (in the given example - alternative writing of the words in Ukrainian) the sed replace performs correctly replacing all occurrences, while python3 re.sub() function replaces only first two
msg135980 - (view) Author: Matthew Barnett (mrabarnett) * (Python triager) Date: 2011-05-14 17:26
Argument 4 of re.sub is the maximum number of replacements, NOT flags:

Help on function sub in module re:

sub(pattern, repl, string, count=0, flags=0)
    Return the string obtained by replacing the leftmost
    non-overlapping occurrences of the pattern in string by the
    replacement repl.  repl can be either a string or a callable;
    if a string, backslash escapes in it are processed.  If it is
    a callable, it's passed the match object and must return
    a replacement string to be used.

re.I is 2, so you're telling it to perform at most 2 replacements.
msg135992 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2011-05-14 21:49
See #11957.
msg135995 - (view) Author: Andriy Rysin (arysin) Date: 2011-05-14 22:13
Ahh, I guess I should have read the docs. But then looks like I am not the fist or only one so it's not as embarrassing. :)

I would support the proposal in #11957 to make re flags non-int object
History
Date User Action Args
2011-05-14 22:13:00arysinsetmessages: + msg135995
2011-05-14 21:49:39ezio.melottisetstatus: open -> closed

superseder: re.sub confusion between count and flags args

nosy: + ezio.melotti
messages: + msg135992
resolution: duplicate
stage: resolved
2011-05-14 17:26:53mrabarnettsetnosy: + mrabarnett
messages: + msg135980
2011-05-14 15:07:00arysinsetmessages: + msg135975
2011-05-14 15:05:40arysinsetfiles: + test.txt
2011-05-14 15:05:27arysinsetfiles: + repl.py
2011-05-14 15:04:53arysincreate