0% found this document useful (0 votes)
2 views14 pages

Regex Basics and String Exercises

The document provides an overview of regular expressions (RegEx) and their basic commands, including functions like re.match(), re.search(), and re.findall(). It covers character classes, quantifiers, lookaheads, lookbehinds, and escape sequences, along with examples and exercises for practical application. The content is aimed at helping readers understand and manipulate strings using RegEx effectively.

Uploaded by

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

Regex Basics and String Exercises

The document provides an overview of regular expressions (RegEx) and their basic commands, including functions like re.match(), re.search(), and re.findall(). It covers character classes, quantifiers, lookaheads, lookbehinds, and escape sequences, along with examples and exercises for practical application. The content is aimed at helping readers understand and manipulate strings using RegEx effectively.

Uploaded by

samyak.mehta300
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

String Manipulation :

RegEx
By,
Prof Sandip A Mehta
Assistant Professor, EI department
Regex : Basic commands
• Import re : Import the regex
• [Link]() : Matching particular String
• [Link]() : Matching particular string at starting and ending of the
string
• [Link]() : find all the matches
Simple code
• Example : whether the sentence contains word “ SFE” or not.
• import re
• reg1=“\bSFE\b”
• my_text=“Hi SFE innovative assignment is pending”
• X=[Link](reg1, my_text)
Character Class
• \d: Matches any digit (equivalent to [0-9]).
• \D: Matches any non-digit.
• \w: Matches any word character (alphanumeric plus underscore,
equivalent to [a-zA-Z0-9_]).
• \W: Matches any non-word character.
• \s: Matches any whitespace character (spaces, tabs, line breaks).
• \S: Matches any non-whitespace character.
Basics of regex quantifiers and
patterns
• - `\b` - Matches a word boundary.
• - `\d` - Matches any digit.
• - `\w` - Matches any word character.
• - `\s` - Matches any whitespace character.
• - `.` - Matches any character except for a newline.
• - `^` - Matches the beginning of a line.
• - `$` - Matches the end of a line.
• - `*` - Matches zero or more of the preceding character. (wild or greedy, be careful)
• - `+` - Matches one or more of the preceding character. (wild or greedy, be careful)
• - `?` - Matches zero or one of the preceding character.
• - `[]` - Matches a single character within the brackets.
• - `()` - Groups together a set of characters.
Lookahead and Lookbehind
• Positive Lookahead: A(?=B) Matches A only if it is followed by B.
• Negative Lookahead: A(?!B) Matches A only if it is not followed by B.
• Positive Lookbehind: (?<=B)A Matches A only if it is preceded by B.
• Negative Lookbehind: (?<!B)A Matches A only if it is not preceded by
B.
• Exercise : \b(?=.*\d)……\b
• \b(?=.*[A-Z])[A-Za-z]+\b
Escape Sequences\:
• Escapes special characters to be matched literally.
• Example: \. matches a literal dot.
More comments on * and +
• - Regex `a*b`: This will match any string that starts with zero or more
'a's, followed by a single 'b'. For example, it would match '', 'b', 'ab',
'aab', 'aaab', and so on.

• - Regex `a+b`: This will match any string that starts with one or more
'a's, followed by a single 'b'. For example, it would match 'ab', 'aab',
'aaab', and so on, but it would not match 'b' or ''.
Square Bracket (special meaning)
More examples for the quantifiers and
combination
Exercises
• 1 Find that particular string is starting with “hi” or not
• 2. Find that particular string is ending with “all” or not
• 3. Find all the words that begin with “nir” .
• 4. Find how many times cat word appears in the given string
• 5. Extract only the digits from the given string.
• 6. Extract the date of birth from the given string
• 7. Extract the email address from the given string
• 8. Extract the roll number from the given string.

You might also like