编写一个函数getWords(sentence, letter),该函数接受一个句子和一个字母,并返回以该字母开头或结尾的单词列表,但不能同时返回,无论字母大小写。
例如:
>>> s = "The TART program runs on Tuesdays and Thursdays, but it does not start until next week."
>>> getWords(s, "t")
['The', 'Tuesdays', 'Thursdays', 'but', 'it', 'not', 'start', 'next']
Run Code Online (Sandbox Code Playgroud)
我的尝试:
regex = (r'[\w]*'+letter+r'[\w]*')
return (re.findall(regex,sentence,re.I))
Run Code Online (Sandbox Code Playgroud)
我的输出:
['The', 'TART', 'Tuesdays', 'Thursdays', 'but', 'it', 'not', 'start', 'until', 'next']
Run Code Online (Sandbox Code Playgroud)
\b检测断字。详细模式允许多行正则表达式和注释。请注意,[^\W]与 相同\w,但要匹配\w某个字母,您需要[^\W{letter}].
import re
def getWords(s,t):
pattern = r'''(?ix) # ignore case, verbose mode
\b{letter} # start with letter
\w* # zero or more additional word characters
[^{letter}\W]\b # ends with a word character that isn't letter
| # OR
\b[^{letter}\W] # does not start with a non-word character or letter
\w* # zero or more additional word characters
{letter}\b # ends with letter
'''.format(letter=t)
return re.findall(pattern,s)
s = "The TART program runs on Tuesdays and Thursdays, but it does not start until next week."
print(getWords(s,'t'))
Run Code Online (Sandbox Code Playgroud)
输出:
['The', 'Tuesdays', 'Thursdays', 'but', 'it', 'not', 'start', 'next']
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10945 次 |
| 最近记录: |