Question

how can I use regex to find a word containing the letter 'a' but it is...

how can I use regex to find a word containing the letter 'a' but it is not followed by the letter 'b'. python

Homework Answers

Answer #1

The entire code is ad follows:-

import re

words = ["abcdef", "acdef", "cdeabc abcd cdeaf cdefa acdef acbfd"]
print(words)

for word in words:
    print(word)
    print("\t", re.findall(r"(\w*a(?!b)\w*)", word))

What we have used here is called negative lookahead. The   (?!) sequence succeeds if it doesn't match the following character, but is not captured.

Explanation as per our problem's regular expression:

\w* is trying to match all the characters or digits before our negative lookahead.

a(?!b) will match all the words that contain the letter a not followed by b.

The following is the output of the above code snippet:

I hope this solved your query. If you have any doubt do let me know. Regards.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Use python programming: Capitalise first letter of each word in a sentence
Use python programming: Capitalise first letter of each word in a sentence
a) How many four-letter words can be formed from the letters of the word TAUDRY if...
a) How many four-letter words can be formed from the letters of the word TAUDRY if each letter can only be used one time in a word? Y is NOT considered a vowel in this word. b) How many contain the letter Y? c) How many contain all the vowels? d) How many contain exactly three consonants? e) How many of them begin and end in a consonant? f) How. many begin with a D and end in a vowel...
a) How many four-letter words can be formed from the letters of the word TAUDRY if...
a) How many four-letter words can be formed from the letters of the word TAUDRY if each letter can only be used one time in a word? Y is NOT considered a vowel in this word. b) How many contain all the vowels? c) How many contain exactly three consonants? d) How many of them begin and end in a consonant? e) How many contain both D and Y?
Randomly taking two letters from the word "INFINITY," let X be the letter of "I." Find...
Randomly taking two letters from the word "INFINITY," let X be the letter of "I." Find the distribution of X.
I. For problem 1-10 enter the letter of the word that best matches the definition 1)...
I. For problem 1-10 enter the letter of the word that best matches the definition 1) _____ In the statement computerPick = “rock”, the = (equal sign) means                               a) number 2) ____ randint in Python is an example of a Python package function and requires an            b) type                  ______ statement at the beginning of your program.                                                       3) ____ The passed arguments in calling a function must match the parameters                        c) parenthesis defined in the function...
. How many different letter arrangements can be made from the letters of the word KALAMAZOO
. How many different letter arrangements can be made from the letters of the word KALAMAZOO
A letter is chosen at random from the word BANANA. Find the odds that the letter...
A letter is chosen at random from the word BANANA. Find the odds that the letter is an A or an N. (Write your answer this way - 4:7 with reduced and with no spaces)
Consider an 18 letter word consisting of 9 As and 9 Bs. How many different words(not...
Consider an 18 letter word consisting of 9 As and 9 Bs. How many different words(not necessarily meaningful) can be produced by these letters? Next, with the same number of A and B, how many combinations of words are there if any combinations of 3As and 6Bs must be in the first 9 letters in the word?
Each letter of the word “supercalifragilisticexpialidocious” is placed into a bag and drawn at 3 times,...
Each letter of the word “supercalifragilisticexpialidocious” is placed into a bag and drawn at 3 times, replacing the letter after each draw. Find the probability that the letter "i" is drawn at least once. Round your answer to two decimal places. Please help. Thanks!
How many words can be formed by arranging the letters of the word “EQUATIONS” such that...
How many words can be formed by arranging the letters of the word “EQUATIONS” such that the first letter of the word is a vowel and the last position is a consonant letter? (Note: The words thus formed need not be meaningful.)
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT