Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Merge branch 'main' into re-properties
# Conflicts:
#	Doc/whatsnew/3.16.rst
#	Lib/re/_parser.py
  • Loading branch information
serhiy-storchaka committed Jun 25, 2026
commit b7ffde6cdc5655729b10f02321935abaa7dfa3fe
8 changes: 8 additions & 0 deletions Doc/whatsnew/3.16.rst
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,14 @@ os
re
--

* :mod:`re` now supports set operations and nested sets in character classes,
as described in `Unicode Technical Standard #18
<https://unicode.org/reports/tr18/>`__: set difference (``[A--B]``),
intersection (``[A&&B]``) and union (``[A||B]``), where an operand may be a
nested set written in square brackets. For example, ``[a-z--[aeiou]]``
matches an ASCII lowercase consonant.
(Contributed by Serhiy Storchaka in :gh:`152100`.)

* Regular expressions now support Unicode property escapes ``\p{...}`` and
``\P{...}``, which match a character by a Unicode property -- for example
``\p{Lu}`` (an uppercase letter), ``\p{Cased}`` or ``\p{ASCII}``. See
Expand Down
98 changes: 12 additions & 86 deletions Lib/re/_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,12 @@ def _parse_operand(source, state, nested, here, allow_nested):
sourcematch = source.match
set = []
setappend = set.append
def addmember(code):
# Flatten a \p{...} property's IN into the member set.
if code[0] is IN:
set.extend(code[1])
else:
setappend(code)
compound = None # elements of a standalone nested-set operand
if allow_nested and sourcematch("["):
# A nested set after an operator is the whole operand, used as-is (not
Expand Down Expand Up @@ -628,13 +634,13 @@ def _parse_operand(source, state, nested, here, allow_nested):
source.tell() - here)
if that == "]":
# A trailing '-' is a literal.
setappend(code1)
addmember(code1)
setappend((LITERAL, _ord("-")))
return [_charset_node(_uniq(set))], None
if that == "-":
# 'X--': difference, not a range. '--' after a single member
# lands here because the range probe consumed the first '-'.
setappend(code1)
addmember(code1)
return [_charset_node(_uniq(set))], "--"
if that[0] == "\\":
code2 = _class_escape(source, that)
Expand All @@ -650,7 +656,7 @@ def _parse_operand(source, state, nested, here, allow_nested):
raise source.error(msg, len(this) + 1 + len(that))
setappend((RANGE, (lo, hi)))
else:
setappend(code1)
addmember(code1)

def _complement(elements, state):
# The complement of `elements` (a single matcher, or a set operation as a
Expand Down Expand Up @@ -763,89 +769,9 @@ def _parse(source, state, verbose, nested, first=False):
subpatternappend((LITERAL, _ord(this)))

elif this == "[":
here = source.tell() - 1
# character set
set = []
setappend = set.append
## if sourcematch(":"):
## pass # handle character classes
if source.next == '[':
import warnings
warnings.warn(
'Possible nested set at position %d' % source.tell(),
FutureWarning, stacklevel=nested + 6
)
negate = sourcematch("^")
# check remaining characters
while True:
this = sourceget()
if this is None:
raise source.error("unterminated character set",
source.tell() - here)
if this == "]" and set:
break
elif this[0] == "\\":
code1 = _class_escape(source, this)
else:
if set and this in '-&~|' and source.next == this:
import warnings
warnings.warn(
'Possible set %s at position %d' % (
'difference' if this == '-' else
'intersection' if this == '&' else
'symmetric difference' if this == '~' else
'union',
source.tell() - 1),
FutureWarning, stacklevel=nested + 6
)
code1 = LITERAL, _ord(this)
if sourcematch("-"):
# potential range
that = sourceget()
if that is None:
raise source.error("unterminated character set",
source.tell() - here)
if that == "]":
if code1[0] is IN:
set.extend(code1[1])
else:
setappend(code1)
setappend((LITERAL, _ord("-")))
break
if that[0] == "\\":
code2 = _class_escape(source, that)
else:
if that == '-':
import warnings
warnings.warn(
'Possible set difference at position %d' % (
source.tell() - 2),
FutureWarning, stacklevel=nested + 6
)
code2 = LITERAL, _ord(that)
if code1[0] != LITERAL or code2[0] != LITERAL:
msg = "bad character range %s-%s" % (this, that)
raise source.error(msg, len(this) + 1 + len(that))
lo = code1[1]
hi = code2[1]
if hi < lo:
msg = "bad character range %s-%s" % (this, that)
raise source.error(msg, len(this) + 1 + len(that))
setappend((RANGE, (lo, hi)))
else:
if code1[0] is IN:
set.extend(code1[1])
else:
setappend(code1)

set = _uniq(set)
# XXX: <fl> should move set optimization to compiler!
if _len(set) == 1 and set[0][0] is LITERAL:
# optimization
if negate:
subpatternappend((NOT_LITERAL, set[0][1]))
else:
subpatternappend(set[0])
charset = _parse_charset(source, state, nested)
if len(charset) == 1:
code = charset[0]
else:
# Wrap a multi-element set operation in a non-capturing group so
# a following quantifier (e.g. [a-z--[aeiou]]+) binds the whole
Expand Down
You are viewing a condensed version of this merge commit. You can view the full changes here.