Using regular expressions in python requires import re;
The function re.search(p,s) checks whether there is a string p in the string s;
^ matches the beginning of the string, $ ends, . matches any character, ? Indicates that the preceding character is optional;
[ghi] means the letter is g, h or i
code:
import re import nltk wordlist = [w for w in nltk.corpus.words.words('en') if w.islower()] result = [w for w in wordlist if re.search('^[adi].j..t..?$',w)] print(result)
output:
['abjectly', 'adjuster', 'dejecta', 'dejected', 'dejectly', 'injector']