How to get a list of matchable characters from a regex class
<p>Given a regex character class/set, how can i get a list of all matchable characters (in python 3). E.g.:</p>

<pre><code>[\dA-C]
</code></pre>

<p>should give</p>

<pre><code>['0','1','2','3','4','5','6','7','8','9','A','B','C']
</code></pre>

<p>You probably hoped to just extract them from the regexp itself, but it's not that easy: Consider specifications like <code>\S</code>, which doesn't match a contiguous range of characters, negated specifications like <code>[^abc\d]</code>, and of course goodies like <code>(?![aeiou])\w</code> (which matches any single letter <em>except</em> the five vowels given). So it's far simpler to just try out each candidate character against your regexp.</p>

<p>But checking all Unicode codepoints is not very practical, both because of the large number of tests and because the result could be a very large list: A character class regexp might contain specifications like <code>\w</code>,
which can match an enormous number of characters from all over the Unicode table. Or it could contain a negated specification like <code>[^abc\d]</code>,
which matches even more.  So let's assume that you can restrict your interest to a particular
subset of the
Unicode range. After consulting a <a href="https://en.wikipedia.org/wiki/Unicode_block#collapsibleTable0" rel="nofollow">table of Unicode ranges</a>,
you might decide, for the sake of example, that you are interested in the ranges [0000-024F]
(Basic and Extended Latin) and [0590-074F] (Hebrew and Arabic).</p>

<p>You can then churn through each of these unicode codepoints,
checking which ones are matched by your regexp:</p>

<pre><code>import re

myregexp = r"[\dA-C]"
interest = [ (0x0000, 0x024F),
             (0x0590, 0x06FF) ]


pattern = re.compile(myregexp)
matched = []    
for low, high in interest:
    matched.extend(chr(p) for p in range(low, high+1) if pattern.match(chr(p)))

&gt;&gt;&gt; print("".join(matched))
0123456789ABCÙ Ù¡Ù¢Ù£Ù¤Ù¥Ù¦Ù§Ù¨Ù©Û°Û±Û²Û³Û´ÛµÛ¶Û·Û¸Û¹
</code></pre>

python regex python-3.x