I have the following code in 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. 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.
findall
finds non-overlapping matches. An easy fix is to change the final \\s
to a lookahead; (?=\\s)
but you'll probably also want to extend the regex to cope with initial and final matches as well.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.