python / python/cpython

re.sub() repl notation explanation is disjointed

Open
#144,884 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

docs topic-regex
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

Documentation

[I'm sending a PR proposing multiple changes, trying to list here the more "objective" issues and leaving my subjective choices to the PR...]

re.sub, also briefly in re.Match.expand.
Information on how repl argument is processed is split between the first (" repl can be a string or a function; if it is a string ...") and second-to-last paragraphs ("In string-type repl arguments, in addition ..."), in a way that is somewhat arbitrary/illogical, and some important subtleties are omitted:

  • "Unknown escapes" are discussed in first paragraph after mentioning only regular python escapes like \n, before sentence on \6 and far before introducing \g<...> in late paragraph!
  • \6 is introduced early but details of how \20 is parsed is added late in \g discussion. Ambiguities vs. octal notation (\02, \200, \2000) are not mentioned. Reader may guess this is like \2 vs. octal in regex notation.
  • The wording suggests "all escapes" supported in Python string literals are processed. Not so — \x\u\U\N aren't, UNLIKE regex notation. This bears on "unknown escape" handling.

Additionally, the final paragraph documenting flags (added in #119960) comes far after "The pattern may be a string or a Pattern", but actually flags param is only allowed when pattern pattern is a string, which can be best explained by moving these together.
[However, that dependence is worth mentioning in several other functions, so perhaps that deserves separate issue/PR?]

Linked PRs
  • gh-144891

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Read the re.sub() and re.Match.expand() documentation sections linked in the issue, then compare the paragraphs describing string and callable repl arguments, escapes, and flags. Done means the repl notation details and escape limitations are presented coherently, with the pattern/flags dependency placed appropriately; check gh-144891 before starting.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
documentation
Issue type
Documentation
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.