[英]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]
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.