python.nlp essay (1) regular expression

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']



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325543818&siteId=291194637