You are on page 1of 14

NAME:- RAUNAK MALKANI DATE:-16/08/2021

UID:- 20BIT032

REGEX ASSIGNMENT

1. ALL TYPES OF REGEX.

METCHARACTERS:

1. [] A set of characters E.g.: "[a-m]":

2. \ Signals a special sequence (can also be used to escape


special characters)
E.g.: "\d"

3. . Any character(except newline character) E.g.: “he..o”

4. ^ Starts with E.g.: “^hello”


5. $ Ends with E.g.: “world$”

6. * Zero or more occurrences E.g.: “aix*”

7. + One or more occurrences E.g.: “aix+”

8. { } Exactly the specified no. of occurrences E.g.: “al{2}”

9. | Either or E.g.: “falls|stays”


SPECIAL SEQUENCES:

1. \A :- Returns a match if the specified characters are at the


beginning of the string

2. \b :- Returns a match where the specified characters are at


the beginning or at the end of a word (the "r" in the beginning
is making sure that the string is being treated as a "raw
string")

3. \B :- Returns a match where the specified characters are


present, but NOT at the beginning (or at the end) of a word
(the "r" in the beginning is making sure that the string is being
treated as a "raw string")

4. \d :- Returns a match where the string contains digits


(numbers from 0-9)
5. \D :- Returns a match where the string DOES NOT contain
digits

6. \s :- Returns a match where the string contains a white space


character

7. \S :- Returns a match where the string DOES NOT contain a


white space character

8. \w :- Returns a match where the string contains any word


characters (characters from a to Z, digits from 0-9, and the
underscore _ character)

9. \W :- Returns a match where the string DOES NOT contain


any word characters
10. \Z :- Returns a match if the specified characters are at
the end of the string

SETS:

1. [arn] :- Returns a match where one of the specified characters


(a, r, or n) are present.

2. [a-n] :- Returns a match for any lower case character,


alphabetically between a and n.
3. [^arn] :- Returns a match for any character EXCEPT a, r, and n.

4. [0123] :- Returns a match where any of the specified digits (0,


1, 2, or 3) are present.

5. [0-9] :- Returns a match for any digit between 0 and 9.

6. [0-5][0-9] :- Returns a match for any two-digit numbers from


00 and 59.
7. [a-zA-Z] :- Returns a match for any character alphabetically
between a and z, lower case OR upper case.

8. [+] :- In sets, +, *, ., |, (), $,{} has no special meaning, so [+]


means: return a match for any + character in the string.

2. Write a Python program that matches a string that has an a


followed by zero or more b's.
Input:-

import re

def text_match(text):
patterns = 'ab+'

if re.search(patterns, text):
return 'Found a match'
else:
return 'Match not found'

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

Output:-

3. Write a Python program that matches a string that has an a


followed by two to three 'b'.
Input:-
import re

def text_match(text):
patterns = 'ab{2,3}'

if re.search(patterns, text):
return 'Found a match'
else:
return 'Match not found'
print(text_match("ac"))
print(text_match("abbbb"))
print(text_match("abc"))

Output:-

4. Write a Python program that matches a string that has an 'a'


followed by anything, ending in 'b'.
Input:-
import re

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

if re.search(patterns, text):
return 'Found a match'
else:
return 'Match not found'
print(text_match("aaabbbd"))
print(text_match("abbbb"))
print(text_match("accddbbjjjb"))

Output:-

5. Write a Python program that matches a word at the beginning


of a string.
Input:-
import re

def text_match(text):
patterns = '^\w+'

if re.search(patterns, text):
return 'Found a match'
else:
return 'Match not found'
print(text_match(" The quick brown fox jumps over the lazy
dog."))
print(text_match("The quick brown fox jumps over the lazy
dog."))

Output:-

6. Write Python program to search the numbers (0-9) of length


between I to 4 in a given string.
Input:-
import re

results = re.finditer(r"([0-9]{1,3})","Examples of number


sentences include: 32+57 = ? 5x6=10x?")

print("number of length 1 to 3")

for n in results:
print(n.group(0))

Output:-
7. Write a Python program to search a literals string in a string
and also find the location within the original string where the
pattern occurs.
Input:-
import re

pattern = 'was'
text = 'The train was late.'
match = re.search(pattern, text)
s = match.start()
e = match.end()

print('Found "%s" in "%s" from %d to %d' % \


(match.re.pattern, match.string, s, e))

Output:-
8. Write a Python program that count the words and spaces in a
string.
Input:-
import re

def string():
sen = str(input('Enter any string:'))
regex = r"(\w+)"
reex = r"(\s)"

match = len(re.findall(regex, sen))


txt = len(re.findall(reex, sen))

print("Total number of words in the given


string",match)
print("Total number of whitespaces in the given
string",txt)
print(string())

Output:-

You might also like