简体   繁体   English

与 findall() 一起使用的正则表达式模式以定位 3 个字母的单词

[英]Regex pattern for use with findall() to locate 3 letter words

So I have some code that produces the right result, but the code is too specific.所以我有一些代码可以产生正确的结果,但是代码太具体了。 I want it more generic so that I can use re.findall() to extract all 3 letter words from 's2' (for example, DOG).我希望它更通用,以便我可以使用 re.findall() 从 's2' 中提取所有 3 个字母的单词(例如,DOG)。

In the below code, the problem is that I am cherry-picking the three words that are 3 letter words when I print m[0:3].在下面的代码中,问题是我在打印 m[0:3] 时挑选了三个字母的三个单词。 In other words, the code I have looks for 3+ letter words and I have to cherry-pick, whereas I just want 3 letters words--no more, no less from the get-go.换句话说,我的代码寻找 3 个以上字母的单词,我必须挑选,而我只想要 3 个字母的单词——从一开始就不多不少。

list2 = 'A, k: it; hi! map@ LAP# rAp, home: LIFE; party! animals@'
m = re.findall(r'(\w{3,})', list2) 
m[0:3]

I want to get ['map', 'lap', 'rAP'].我想得到 ['map', 'lap', 'rAP']。 And I want for the code to have ability to retain any other 3, no more no less, letter words in the future.而且我希望代码能够在未来保留任何其他 3 个字母单词,不多不少。

You can look for exactly three word characters, surrounded by word boundaries:您可以准确地查找三个单词字符,它们被单词边界包围:

import re

s2 = 'I, a: be; go! cat@ DOG# aRe, home: WORK; ninja! rules@'
m = re.findall(r'\b\w{3}\b', s2) 
print(m)
# ['cat', 'DOG', 'aRe']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM