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 mu
(pattern, repl, string, *args, count=_zero_sentinel, flags=_zero_sentinel)
| 183 | _zero_sentinel = _ZeroSentinel() |
| 184 | |
| 185 | def sub(pattern, repl, string, *args, count=_zero_sentinel, flags=_zero_sentinel): |
| 186 | """Return the string obtained by replacing the leftmost |
| 187 | non-overlapping occurrences of the pattern in string by the |
| 188 | replacement repl. repl can be either a string or a callable; |
| 189 | if a string, backslash escapes in it are processed. If it is |
| 190 | a callable, it's passed the Match object and must return |
| 191 | a replacement string to be used.""" |
| 192 | if args: |
| 193 | if count is not _zero_sentinel: |
| 194 | raise TypeError("sub() got multiple values for argument 'count'") |
| 195 | count, *args = args |
| 196 | if args: |
| 197 | if flags is not _zero_sentinel: |
| 198 | raise TypeError("sub() got multiple values for argument 'flags'") |
| 199 | flags, *args = args |
| 200 | if args: |
| 201 | raise TypeError("sub() takes from 3 to 5 positional arguments " |
| 202 | "but %d were given" % (5 + len(args))) |
| 203 | |
| 204 | import warnings |
| 205 | warnings.warn( |
| 206 | "'count' is passed as positional argument", |
| 207 | DeprecationWarning, stacklevel=2 |
| 208 | ) |
| 209 | |
| 210 | return _compile(pattern, flags).sub(repl, string, count) |
| 211 | sub.__text_signature__ = '(pattern, repl, string, count=0, flags=0)' |
| 212 | |
| 213 | def subn(pattern, repl, string, *args, count=_zero_sentinel, flags=_zero_sentinel): |