简体   繁体   English

找3个字母的单词

[英]Find 3 letter words

I have the following code in Python: 我在Python中有以下代码:

import re
string = "what are you doing you i just said hello guys"
regexValue = re.compile(r'(\s\w\w\w\s)')
mo = regexValue.findall(string)

My goal is to find any 3 letter word, but for some reason I seem to only be getting the "are" and not the "you" in my list. 我的目标是找到任何3个字母的单词,但由于某种原因,我似乎只是在我的列表中得到“是”而不是“你”。 I figured this might be because the space between the two overlap, and since the space is already used it cannot be a part of "you". 我想这可能是因为两者之间的空间重叠,并且因为空间已经被使用,所以它不能成为“你”的一部分。 So, how should I find only three letter words from a string like this? 那么,我怎么才能找到像这样的字符串中的三个字母单词呢?

这不是正则表达式,但你可以这样做:

words = [word for word in string.split() if len(word) == 3]

You should use word boundary (\\b\\w{3}\\b) if you strictly want to use regex otherwise, answer suggested by Morgan Thrapp is good enough for this. 如果您严格要使用正则表达式,则应使用单词边界(\\b\\w{3}\\b) ,否则,Morgan Thrapp建议的答案就足够了。

Demo 演示

findall finds non-overlapping matches. findall找到不重叠的匹配。 An easy fix is to change the final \\s to a lookahead; 一个简单的解决方法是将最终\\s更改为预测; (?=\\s) but you'll probably also want to extend the regex to cope with initial and final matches as well. (?=\\s)但你可能也想扩展正则表达式以应对初始和最终匹配。

regexValue = re.compile(r'((?:^\s)\w\w\w(?: $|(?=\s))')

If this is not a regex exercise, splitting the string on whitespace is much mose straightforward. 如果这不是正则表达式练习,那么在空格上拆分字符串非常简单。

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

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