0% found this document useful (0 votes)
11 views9 pages

Regular Expression

The document contains a series of Python programs that utilize regular expressions to match specific patterns in strings. Each program demonstrates different regex functionalities, such as checking allowed characters, matching sequences of letters, and identifying patterns with specific conditions. The document includes example inputs and outputs for each program to illustrate their effectiveness.

Uploaded by

rythwiniphone
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views9 pages

Regular Expression

The document contains a series of Python programs that utilize regular expressions to match specific patterns in strings. Each program demonstrates different regex functionalities, such as checking allowed characters, matching sequences of letters, and identifying patterns with specific conditions. The document includes example inputs and outputs for each program to illustrate their effectiveness.

Uploaded by

rythwiniphone
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Regular expression

Debang Debata
24BEC1089

1. Write a Python program to check that a string contains only a certain set of
characters (in this case a-z, A-Z and 0-9).

import re
def is_allowed_specific_char(string):
charRe = re.compile(r'[^a-zA-Z0-9]')
string = charRe.search(string)
return not bool(string)

print(is_allowed_specific_char("ABCDEFabcdef123450"))
print(is_allowed_specific_char("*&%@#!}{"))

2. Write a Python program that matches a string that has an a followed by zero
or more b's.

import re
def text_match(text):
patterns = '^a(b*)$'
if re.search(patterns, text):
return 'Found a match!'
else:
return('Not matched!')
print(text_match("ac"))
print(text_match("abc"))
print(text_match("a"))
print(text_match("ab"))
print(text_match("abb"))

3. Write a Python program that matches a string that has an a followed by one or
more b's.

import re
def text_match(text):
patterns = 'ab+?'
if re.search(patterns, text):
return 'Found a match!'
else:
return('Not matched!')

print(text_match("ab"))
print(text_match("abc"))

4. Write a Python program that matches a string that has an a followed by zero
or one 'b'.

import re
def text_match(text):
patterns = 'ab?'
if re.search(patterns, text):
return 'Found a match!'
else:
return('Not matched!')

print(text_match("ab"))
print(text_match("abc"))
print(text_match("abbc"))
print(text_match("aabbc"))

5. Write a Python program that matches a string that has an a followed by three
'b'.

import re
def text_match(text):
patterns = 'ab{3}?'
if re.search(patterns, text):
return 'Found a match!'
else:
return('Not matched!')

print(text_match("abbb"))
print(text_match("aabbbbbc"))
6. Write a Python program that matches a string that has an a followed by two to
three 'b'.

import re
def text_match(text):
patterns = 'ab{2,3}'
if re.search(patterns, text):
return 'Found a match!'
else:
return('Not matched!')
print(text_match("ab"))
print(text_match("aabbbbbc"))
7. Write a Python program to find sequences of lowercase letters joined by an
underscore.

import re
def text_match(text):
patterns = '^[a-z]+_[a-z]+$'
if re.search(patterns, text):
return 'Found a match!'
else:
return('Not matched!')

print(text_match("aab_cbbbc"))
print(text_match("aab_Abbbc"))
print(text_match("Aaab_abbbc"))

8. Write a Python program to find the sequences of one upper case letter
followed by lower case letters.

import re
def text_match(text):
patterns = '[A-Z]+[a-z]+$'
if re.search(patterns, text):
return 'Found a match!'
else:
return('Not matched!')
print(text_match("AaBbGg"))
print(text_match("Python"))
print(text_match("python"))
print(text_match("PYTHON"))
print(text_match("aA"))
print(text_match("Aa"))

9. Write a Python program that matches a string that has an 'a' followed by
anything ending in 'b'.

import re

def text_match(text):
patterns = 'a.*?b$'

if re.search(patterns, text):

return 'Found a match!'

else:

return('Not matched!')

print(text_match("aabbbbd"))

print(text_match("aabAbbbc"))

print(text_match("accddbbjjjb"))

10. Write a Python program that matches a word at the beginning of a string.

import re

def text_match(text):

patterns = '^\w+'

if re.search(patterns, text):

return 'Found a match!'

else:
return('Not matched!')

print(text_match("The quick brown fox jumps over the lazy dog."))

print(text_match(" The quick brown fox jumps over the lazy dog."))

You might also like