From 91922f0793b29258bc36bc4bf74df6a107802749 Mon Sep 17 00:00:00 2001 From: Stevoisiak Date: Wed, 8 Feb 2023 13:29:10 -0500 Subject: [PATCH 1/2] Explain using Match.expand with \g<0> Update documentation for `re` library to explain that a backreference to group0 (`\g<0>`) is expanded to the entire string when using `Match.expand()`. Note that numeric backreferences to group 0 (`\0`) are not supported. (See https://stackoverflow.com/q/58134893/3357935) --- Doc/library/re.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Doc/library/re.rst b/Doc/library/re.rst index d0a16b95184474..89a94d30942fdd 100644 --- a/Doc/library/re.rst +++ b/Doc/library/re.rst @@ -1264,7 +1264,8 @@ Match objects support the following methods and attributes: Escapes such as ``\n`` are converted to the appropriate characters, and numeric backreferences (``\1``, ``\2``) and named backreferences (``\g<1>``, ``\g``) are replaced by the contents of the - corresponding group. + corresponding group. A named backreference to *group0* (``\g<0>``) will be + replaced by the entire match. .. versionchanged:: 3.5 Unmatched groups are replaced with an empty string. From ded1c1301ff81d86758b7c5aa2cb210ebc0b7f5e Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Sat, 17 Feb 2024 10:27:06 +0200 Subject: [PATCH 2/2] Update Doc/library/re.rst --- Doc/library/re.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/re.rst b/Doc/library/re.rst index 5e27e7aca2ee63..0336121c2bc631 100644 --- a/Doc/library/re.rst +++ b/Doc/library/re.rst @@ -1344,7 +1344,7 @@ when there is no match, you can test whether there was a match with a simple Escapes such as ``\n`` are converted to the appropriate characters, and numeric backreferences (``\1``, ``\2``) and named backreferences (``\g<1>``, ``\g``) are replaced by the contents of the - corresponding group. A named backreference to *group0* (``\g<0>``) will be + corresponding group. The backreference ``\g<0>`` will be replaced by the entire match. .. versionchanged:: 3.5