Issue 12078: re.sub() replaces only several matches
Issue12078
Created on 2011-05-14 15:04 by arysin, last changed 2022-04-11 14:57 by admin. 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) * ![]() |
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) * ![]() |
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 |
| 2022-04-11 14:57:17 | admin | set | github: 56287 |
| 2011-05-14 22:13:00 | arysin | set | messages: + msg135995 |
| 2011-05-14 21:49:39 | ezio.melotti | set | status: open -> closed superseder: re.sub confusion between count and flags args nosy:
+ ezio.melotti |
| 2011-05-14 17:26:53 | mrabarnett | set | nosy:
+ mrabarnett messages: + msg135980 |
| 2011-05-14 15:07:00 | arysin | set | messages: + msg135975 |
| 2011-05-14 15:05:40 | arysin | set | files: + test.txt |
| 2011-05-14 15:05:27 | arysin | set | files: + repl.py |
| 2011-05-14 15:04:53 | arysin | create | |

