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: []
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.
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 $.
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.
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*
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']
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With