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