Repository navigation
Support \u and \U escapes in regexes #47915
Description
Activity
Since \u and \U aren't interpolated in raw strings anymore, the re
module should support those escapes in addition to the \x and octal ones
it already does. Attached patch.- addedtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or errorstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory
on Aug 24, 2008 - Check that it also works for chars > 0xFFFF (even in UCS2 builds, at
least when the chars are not part of [character range]) - What does happen with e.g. [\U00010000-\U00010001] on an UCS build?
- Check that it also works for chars > 0xFFFF (even in UCS2 builds, at
(in the last sentence, I meant UCS2. Sorry)
These concerns indeed must be handled: On narrow unicode builds, chars >
0xffff must be converted to surrogates. In ranges, they should raise an
error.Additionally, this should at least raise an error too:
>>> re.compile("[\U00100000]").match("\U00100000").group() '\udbc0'
Here's an updated patch for py3k branch.
As per Georg's comment, I added to check codepoint in the character
ranges, conversion to the surrogate pairs. I also added check to raise
exception if codepoint > 0x10ffff.
I with to English speakers to fix error messages in the patch.FYI,
+ raise error("bogus escape: %s" % repr(escape))can be written simply as
+ raise error("bogus escape: %r" % escape)
I don't think it is worth to target it for 2.7 and 3.2 (it's new feature, not bugfix), but for 3.3 it will be very useful.
Since PEP-393 conversion to the surrogate pairs is no longer relevant.
- addedtype-featureA feature request or enhancementA feature request or enhancementand removedtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
on Jun 1, 2012 Georg, Atsuo, how are you?
Here is updated (in conforming with PEP-393) patch. In additional octal and hexadecimal escaping cleared, illegal error message for hexadecimal escaping fixed. Added new tests for octal and hexadecimal escaping.
I forgot about byte patterns. Here is an updated patch.
Any chance to commit the patch today and to get this feature in Python 3.3?
New changeset b1dbd8827e79 by Antoine Pitrou in branch 'default':
Issue bpo-3665: \u and \U escapes are now supported in unicode regular expressions.
http://hg.python.org/cpython/rev/b1dbd8827e79Any chance to commit the patch today and to get this feature in Python
3.3?Thanks for reminding us! It's now in 3.3.
Thank you for the quick response.
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields: