【发布时间】:2015-06-23 17:35:39
【问题描述】:
我正在尝试在正则表达式中查找长度为 4 的单词
我正在尝试这个,但我得到一个空列表:
#words that have length of 4
s = input("please enter an expression: ")
print(re.findall(r'/^[a-zA-Z]{4}$/',s))
我的代码有什么问题?
我的输入是:here we are having fun these days
我的预期输出:['here', 'days']
我的输出:[]
【问题讨论】:
-
通过添加
^和$,您要求 整个 字符串是长度为 4 的单词,而不是在其中找到长度为 4 的所有单词。 -
检查正则表达式 \b 运算符而不是 ^ 和 $
-
@AvinashRaj 请检查编辑
标签: python regex python-3.x