使用正则表达式查找长度为4的单词

Moz*_*ein 2 python regex python-3.x

我试图在长度为4的正则表达式中找到单词

我正在尝试这个,但我得到一个空列表:

#words that have length of 4
s = input("please enter an expression: ")
print(re.findall(r'/^[a-zA-Z]{4}$/',s))
Run Code Online (Sandbox Code Playgroud)

我的代码出了什么问题?

我的意见是: here we are having fun these days

我的预期输出: ['here', 'days']

我的输出: []

Avi*_*Raj 8

使用单词边界\b.当你在正则表达式中添加锚时^[a-zA-Z]{4}$,这将匹配只有四个字母的行.它不会检查每个单词.^声称我们刚开始并$声称我们已经结束了.\b单词字符和非单词字符之间的匹配(反之亦然).因此它匹配单词的一个单词或结尾(零宽度)的开始(零宽度).

>>> s = "here we are having fun these days"
>>> re.findall(r'\b[a-zA-Z]{4}\b', s)
['here', 'days']
Run Code Online (Sandbox Code Playgroud)