PyDocs

Nav

Text Processing Services

sub

re

function (source) Deprecated 3.13
  • sub(pattern, repl, string, count=0, flags=0)
Deprecated since 3.13. Passing count and flags as positional arguments is deprecated. In future Python versions they will be keyword-only parameters.
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.
Changed in 3.1, 3.5, 3.6, 3.7, 3.12
  • 3.1: Added the optional flags argument.
  • 3.5: Unmatched groups are replaced with an empty string.
  • 3.6: Unknown escapes in pattern consisting of '\' and an ASCII letter now are errors.
  • 3.7: Unknown escapes in repl consisting of '\' and an ASCII letter now are errors. An empty match can occur immediately after a non-empty match.
  • 3.12: Group id can only contain ASCII digits. In bytes replacement strings, group name can only contain bytes in the ASCII range (b'\x00'-b'\x7f').
1 Example
>>> re.sub(r'def\s+([a-zA-Z_][a-zA-Z_0-9]*)\s*\(\s*\):',
...        r'static PyObject*\npy_\1(void)\n{',
...        'def myfunc():')
'static PyObject*\npy_myfunc(void)\n{'
from the Python docs
See Also
No see-alsos for re/sub
0 Notes
No notes for sub