5

I am trying to find words in regular expression with length 4

I am trying this but I am getting an empty list:

#words that have length of 4
s = input("please enter an expression: ")
print(re.findall(r'/^[a-zA-Z]{4}$/',s))

What is wrong with my code ?

my input is: here we are having fun these days

my expected output: ['here', 'days']

my output: []

rfornal
  • 5,072
  • 5
  • 30
  • 42
Mozein
  • 787
  • 5
  • 19
  • 33

2 Answers2

12

Use word boundaries \b. When you add anchors in your regex like ^[a-zA-Z]{4}$, this would match the lines which have only four alphabets. It won't check for each individual words. ^ asserts that we are at the start and $ asserts that we are at the end. \b matches between a word character and a non-word character(vice versa). So it matches the start (zero width) of a word or end (zero width) of a word.

>>> s = "here we are having fun these days"
>>> re.findall(r'\b[a-zA-Z]{4}\b', s)
['here', 'days']
Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
1

No need for a (possibly) complicated , you can just use a list comprehension:

>>> s = "here we are having fun these days"
>>> [word for word in s.split() if len(word) == 4 and word.isalpha()]
['here', 'days']
>>> 
A.J. Uppal
  • 19,117
  • 6
  • 45
  • 76
  • it must be `[word for word in s.split() if len(word) == 4 and word.isalpha()]` – Avinash Raj Apr 17 '15 at 03:35
  • 1
    This is fine, but this is likely to be generally much slower than the regexp solution. The regexp is really not complicated, as far as regular expressions go (and regular expressions are too useful to not be learned). – Eric O. Lebigot Apr 17 '15 at 03:47