This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: re.sub does only first 16 replacements if re.S is used
Type: behavior Stage:
Components: Regular Expressions Versions: Python 2.7
Status: closed Resolution: duplicate
Dependencies: Superseder: re.sub confusion between count and flags args
View: 11957
Assigned To: Nosy List: ezio.melotti, georg.brandl, mgdelmonte, mrabarnett, peter.otten, serhiy.storchaka, vstinner
Priority: normal Keywords:

Created on 2014-10-29 15:35 by mgdelmonte, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (5)
msg230216 - (view) Author: Michael Del Monte (mgdelmonte) Date: 2014-10-29 15:35
Easily reproduced:

re.sub('x', 'a', "x"*20, re.S)

returns 'aaaaaaaaaaaaaaaaxxxx'
msg230217 - (view) Author: Peter Otten (peter.otten) * Date: 2014-10-29 15:49
This is not a bug; the signature of re.sub() is

sub(pattern, repl, string, count=0, flags=0)

and the fourth argument thus the 'count' delimiting the number of substitutions that you accidentally specify as

>>> import re
>>> re.S

I recommend that you use a keyword arg to fix your code:

>>> re.sub('x', 'a', "x"*20, flags=re.S)
msg230218 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2014-10-29 15:51
The fourth parameter is not "flags", but "count", the max. number of substitutions.  "flags" is the fifth parameter, or give it as a keyword argument.
msg230219 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2014-10-29 15:53
This bug report is common. An enhancement would be to make the count parameter a keyword only parameter. Would it break a lot of code?
msg230220 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2014-10-29 15:54
See #11957.
Date User Action Args
2022-04-11 14:58:09adminsetgithub: 66949
2014-10-29 16:09:48vstinnersetsuperseder: re.sub confusion between count and flags args
resolution: not a bug -> duplicate
2014-10-29 15:54:28ezio.melottisetmessages: + msg230220
2014-10-29 15:53:06vstinnersetnosy: + vstinner
messages: + msg230219
2014-10-29 15:51:11georg.brandlsetstatus: open -> closed

nosy: + georg.brandl
messages: + msg230218

resolution: not a bug
2014-10-29 15:49:42peter.ottensetnosy: + peter.otten
messages: + msg230217
2014-10-29 15:37:22pitrousetnosy: + serhiy.storchaka
2014-10-29 15:35:07mgdelmontecreate