Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

find words of length 4 using regular expression

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: []

like image 491
Mozein Avatar asked Apr 17 '15 03:04

Mozein


People also ask

How do I find a word in a regular expression?

The regular expression \b[A]\w+ can be used to find all words in the text which start with A. The \b means to begin searching for matches at the beginning of words, the [A] means that these matches start with the letter A, and the \w+ means to match one or more word characters.

How do you find the length of a regular expression?

To check the length of a string, a simple approach is to test against a regular expression that starts at the very beginning with a ^ and includes every character until the end by finishing with a $.

Which are 3 uses of regular expression?

Regular expressions are used in search engines, in search and replace dialogs of word processors and text editors, in text processing utilities such as sed and AWK, and in lexical analysis.

What is example of regular expression?

Solution: As we know, any number of a's means a* any number of b's means b*, any number of c's means c*. Since as given in problem statement, b's appear after a's and c's appear after b's. So the regular expression could be: R = a* b* c*


1 Answers

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']
like image 196
Avinash Raj Avatar answered Sep 23 '22 00:09

Avinash Raj