Write a code to match a string that has the letter ‘a’ followed by 4 to 6 'b’s.

Last updated 2 years, 11 months ago | 1166 views 75     5

Tags:- Python

We can use the python re module to match the string

import re

def check(txt):
    # regex pattern
    pattern = 'ab{4,8}'

    if re.search(pattern,  txt):
        print('Match found')
    else:
        print('Match not found')

check("abcbbbb")
# Match not found
       
check("aabbbbbc")
# Match found

check("abcbbabbbbbca")
# Match found