Linus Johansson
Linus Johansson

Reputation: 71

Find 3 letter words

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?

Upvotes: 4

Views: 5003

Answers (3)

dnit13
dnit13

Reputation: 2496

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.

Demo

Upvotes: 6

tripleee
tripleee

Reputation: 189749

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.

Upvotes: 1

Morgan Thrapp
Morgan Thrapp

Reputation: 9986

It's not regex, but you could do this:

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

Upvotes: 8

Related Questions