Regex to return set of words from file that can be spelled with letters passed as parameter (python)
<p>I have a list of words such as </p>

<pre><code>name
age
abhor
apple
ape
</code></pre>

<p>I want to do regex on a list by passing a random set of letters  such as 'apbecd'</p>

<p>Now all those words from the list having the set of letters must be returned.</p>

<p>eg: <code>python retun_words.py apbelcdg</code></p>

<p>will return </p>

<pre><code>ape
apple
age
</code></pre>

<p>As of now, i am able to return words based only on words match. How can I achieve the results as I have mentioned above.
also if there is any other way to achieve the results instead of regex kindly let me know</p>

<p>Thanks in advance</p>

<p>Here, using set and returning the item is also a way, if you do not want to use regex.</p>

<pre><code>string_list = ["name", "age", "abhor", "apple", "ape"]
allowed_characters = "apbelcdg"
character_set = set(allowed_charcters)
print [item for item in string_list if not set(item)-character_set]
</code></pre>

<p>This will give you the list of strings that adhere to the character set.</p>

<p>However, if regex is what you desire the most, here we go :-)</p>

<pre><code>from re import match
string_list = ["name", "age", "abhor", "apple", "ape"]
allowed_characters = "apbelcdg"
print [item for item in string_list if match('[%s]*$' % (allowed_characters), item)]
</code></pre>

python regex