Download as pdf or txt
Download as pdf or txt
You are on page 1of 19

Module 5: Regular Expression

Dr. Anu Singha


Assistant Professor, Faculty of Engineering and Technology
Sri Ramachandra University, Chennai.

1
Wildcards in Python Regular Expression

• A wildcard is a symbol used to replace or represent one or more


characters.
• Wildcards are used in computer programs, languages, search
engines, and operating systems to simplify search criteria.

• The most common wildcards are the asterisk * and the question
mark ?.
Continue…
The asterisk(*)

• An asterisk * is used to specify any number of characters. It is


typically used at the end of a root word. This is great when you
want to search for variable endings of a root word.
• For example, searching for work* would tell the database to look
for all possible word-endings to the root “work”.
Continue…
The question mark(?)

• A question mark ? is used to represent a single character, anywhere


in the word. It is most useful when there are variable spellings for a
word, and you want to search for all variants at once.

• For example, searching for col?r would return “color”.

• The dot (.) character is used in place of the question mark ?


Symbol.
• Similarly .+ characters are used to match one or more characters
(like the asterisk * symbol)
Tutorials
Continue…
Metacharacters
[]-Square Brackets in Regular expressions
Continue…
Continue…
Continue…
Continue…
() - Parenthesis in Regular expressions

• It is called group in regex which is a part of a regex pattern enclosed


in parentheses () metacharacter.
• For example, the regular expression (cat) creates a single group
containing the letters ‘c’, ‘a’, and ‘t’.
• For example, in a real-world case, you want to capture emails and
phone numbers, So you should write two groups, the first will
search email, and the second will search phone numbers.

• They are created by placing the characters to be grouped inside a set of


parentheses (, ).
• For example, in the expression, ((\w)(\s\d)), there are three such groups
((\w)(\s\d))
(\w)
(\s\d)
Continue…
Continue…
| -Alteration in Regular expressions
Continue…
Continue…
^ -caret in Regular expressions
Continue…
Continue…
$ -dollar in Regular expressions
Continue…
Continue…

** The backslash (‘\’) suppresses the special meaning of the character it precedes,
and turns it into an ordinary character. To insert a backslash into your regular
expression pattern, use a double backslash ('\\').

You might also like