Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python re matching fails to work for extened unicode range

import re

pat = re.compile(r"[\u20000-\u2A6D6]+")

pat.match("Hello World!")

This will give us a result

<re.Match object; span=(0, 5), match='Hello'>

But in fact, the input string here is fully ASCII which is not from the unicode range.

Is this expected? If so, how to compile those unicode range in practice?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The pattern currently describes a character class consisting of either \u2000, any character in the range 0-\u2A6D or 6.

For python character literals that are wider than 2 bytes, you need to use the escape sequence \U with 8 hex digits:

pat = re.compile(r"[\U00020000-\U0002A6D6]+")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading