Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>To support characters that can span several Unicode codepoints:</p> <pre><code># -*- coding: utf-8 -*- import re import unicodedata from functools import partial NFKD = partial(unicodedata.normalize, 'NFKD') def match(word, letters): word, letters = NFKD(word), map(NFKD, letters) # normalize return re.match(r"(?:%s)+$" % "|".join(map(re.escape, letters)), word) words = [u"மரம்", u"மடம்", u"படம்", u"பாடம்"] get_words = lambda letters: [w for w in words if match(w, letters)] print(" ".join(get_words([u'ம', u'ப', u'ட', u'ம்']))) # -&gt; மடம் படம் print(" ".join(get_words([u'ப', u'ம்', u'ட']))) # -&gt; படம் </code></pre> <p>It assumes that the same character can be used zero or more times in a word.</p> <p>If you want only words that contain exactly given characters:</p> <pre><code>import regex # $ pip install regex chars = regex.compile(r"\X").findall # get all characters def match(word, letters): return sorted(chars(word)) == sorted(letters) words = ["cat", "dog", "tack", "coat"] print(" ".join(get_words(['o', 'c', 'a', 't']))) # -&gt; coat print(" ".join(get_words(['k', 'c', 't', 'a']))) # -&gt; tack </code></pre> <p>Note: there is no <code>cat</code> in the output in this case because <code>cat</code> doesn't use all given characters.</p> <hr> <blockquote> <p>What does it mean to normalize? And could you please explain the syntax of the re.match() regex?</p> </blockquote> <pre><code>&gt;&gt;&gt; import re &gt;&gt;&gt; re.escape('.') '\\.' &gt;&gt;&gt; c = u'\u00c7' &gt;&gt;&gt; cc = u'\u0043\u0327' &gt;&gt;&gt; cc == c False &gt;&gt;&gt; re.match(r'%s$' % (c,), cc) # do not match &gt;&gt;&gt; import unicodedata &gt;&gt;&gt; norm = lambda s: unicodedata.normalize('NFKD', s) &gt;&gt;&gt; re.match(r'%s$' % (norm(c),), norm(cc)) # do match &lt;_sre.SRE_Match object at 0x1364648&gt; &gt;&gt;&gt; print c, cc Ç Ç </code></pre> <p>Without normalization <code>c</code> and <code>cc</code> do not match. The characters are from the <a href="http://docs.python.org/2.7/library/unicodedata.html#unicodedata.normalize" rel="nofollow"><code>unicodedata.normalize()</code> docs</a>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload