DOC: re discourage single char class #24123
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
this seemed simple enough not to justify a cognate issue,
but it is a regex matter, so maybe it will need one...
there are a few cases in the
re
module documentationthat suggest "indirectly" escaping single metacharacters
by placing them in a character class; i.e.,
[|]
as a viablealternative to
\|
in general, this is likely best to avoid because you can
incur the overhead of a character class with none of the
benefits of a multi-character character class
this is specifically discussed in the more detailed
reference cited by the
re
module docs; in particular,see Chapter 6 of:
Friedl, Jeffrey. Mastering Regular Expressions. 3rd ed.,
O’Reilly Media, 2009.
on the topic of using classes to escape metacharacters, which is
slightly more justified than single (non-meta)character classes,
the author notes
...it's probably because the author didn't know about escaping...
While that may be a little harsh,
avoiding the overhead still makes sense to me.
the docs already cover the fact that most metacharacters
are deactivated inside character classes, so that general information
is still presented to the user clearly, but now without explicitly
suggesting that the mechanism be used for single (meta)characters