Skip to content

gh-152100: Support set operations in character classes - #152153

Merged
serhiy-storchaka merged 1 commit into
python:mainfrom
serhiy-storchaka:re-set-ops-stage1
Jun 25, 2026
Merged

serhiy-storchaka merged 1 commit into
python:mainfrom
serhiy-storchaka:re-set-ops-stage1

Conversation

@serhiy-storchaka

@serhiy-storchaka serhiy-storchaka commented Jun 25, 2026 •

Copy link
Copy Markdown
Member

Implement UTS #18 RL1.3 set operations in regular expression character classes: difference [A--B], intersection [A&&B] and union [A||B], with nested, complemented and compound operands and arbitrary left-to-right chaining ([a-z--[aeiou]&&\w]). Symmetric difference [A~~B] remains reserved (still a FutureWarning).

These tokens (--, &&, ||, and nested [) have been reserved since 3.7 via FutureWarning, so this turns the warnings into real syntax.

The feature is parser-only: each operation maps to an assertion on the matched character — [A--B] → A(?<![B]), [A&&B] → A(?<=[B]), [A||B] → a merged charset [AB] or an alternation — so no charset complement is ever formed and the result is correct under IGNORECASE. A leading ^ negates by De Morgan, pushing the negation into the operands. The engine and MAGIC are untouched.

A [ opens a nested set only immediately after a set operator; at the start of a class it stays an ordinary literal, so existing patterns such as [[:alpha:]] and glob's [*?[] keep their meaning.

The new syntax is also used in the standard library (_strptime, textwrap, doctest, pkgutil).

🤖 Generated with Claude Code

Implement set difference [A--B], intersection [A&&B] and union [A||B] in
regular expression character classes (Unicode Technical Standard #18),
including nested, complemented and compound set operands.  Symmetric
difference [A~~B] remains reserved.

Also use the new syntax in the standard library (_strptime, textwrap,
doctest, pkgutil).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@read-the-docs-community

Copy link
Copy Markdown

Documentation build overview

📚 cpython-previews | 🛠️ Build #33302005 | 📁 Comparing f9a4807 against main (3db3bba)

  🔍 Preview build  

3 files changed
± library/re.html
± whatsnew/3.16.html
± whatsnew/changelog.html
@serhiy-storchaka
serhiy-storchaka merged commit bd4bd3e into python:main Jun 25, 2026
52 checks passed
@serhiy-storchaka
serhiy-storchaka deleted the re-set-ops-stage1 branch June 25, 2026 07:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

1 participant