What is Regular Expression in Python?

Python Regular Expressions or Python RegEx are patterns that permit us to ‘match’ various string values in a variety of ways.

A pattern is simply one or more characters that represent a set of possible match characters. In regular expression matching, we use a character (or a set of characters) to represent the strings we want to match in the text.

Watch this video on Python:

Python RegEx What is Regular Expression in Python? Python Regular Expressions or Python RegEx are patterns that permit us to ‘match’ various string values in a variety of ways. A pattern is simply one or more characters that represent a set of possible match characters. In regular expression matching, we use a

Are you interested in learning Python from experts? Enroll in our Python Course in Bangalore now!

In this Python RegEx tutorial, we will learn all the important aspects of Regular Expression or RegEx in Python, covering the following topics:

Let us get started then.

Regular Expression Characters in Python

SymbolMeaning
. (period)Matches any character except the newline character in a given regular expression in Python
^ (caret sign)Matches the start of any string of a given regular expression in Python
$ (dollar sign)Matches the end of any string of a given regular expression in Python
* (asterisk)Matches zero or more repetitions of a given regular expression in Python
?Matches zero or one of the previous regular expressions in Python
{}Used as either {m}, where m means to match exactly ‘m’ instances of the previous regular expression, or as {m,n} where n > m, meaning to match between ‘m’ and ‘n’ instances of the previous regular expression in Python
\ (backslash)Either a special character, such as one of the other regular expression characters (i.e., \* matches an asterisk) or one of the special regular expression sequences

Learn more about Python from this Python for Data Science Course to get ahead in your career!

Match Function of RegEx in Python

The match function matches the Python RegEx pattern to the string with optional flags.
Syntax:

re.match(pattern, string, flags=0)

Where ‘pattern’ is a regular expression to be matched, and the second parameter is a Python String that will be searched to match the pattern at the starting of the string.

Example of Python regex match:

import re
print re.match(“i”, “intellipaat”)


Output:
<-sre.SRE-Match object at 0x7f9cac95d78>

Python then outputs a line signifying that a new object, i.e., sre.SRE type has been created. The hex number following is the address at which it was created.

import reprint re.match(“b”, “intellipaat”)


Output:
None

Special Sequence Characters of RegEx in Python

Six most important sequence characters are:

  • \d: Matches any decimal digit. This is really the same as writing [0-9] but is done so often that it has its own shortcut sequence.
  • \D: Matches any non-decimal digit. This is the set of all characters that are not in [0-9] and can be written as [^0-9].
  • \s: Matches any white space character. White space is normally defined as a space, carriage return, tab, and non-printable character. Basically, white space is what separates words in a given sentence.
  • \S: Matches any non-white space character. This is simply the inverse of the \s sequence mentioned above.
  • \w: Matches any alphanumeric character. This is the set of all letters and numbers in both lower and uppercase.
  • \W: Matches any non-alphanumeric character. This is the inverse of the \w sequence mentioned above.

Interested in learning Python? Enroll in our Python Course in London now!

Search Function of RegEx in Python

It searches for the primary occurrence of a Regular Expression pattern within a string with optional flags.

Syntax:

re.search(pattern, string, flags=0)

Example of Python regex search:

m = re.search(‘\bopen\b’, ‘please open the door’)
print m


Output:
None

This output so occurred because the ‘\b’ escape sequence is treated as a special backspace character. Meta characters are those characters which include ‘/’.

import re
m = re.search(‘\\bopen\\b’, “please open the door”)
print m


Output:
<-sre.SRE-Match object at 0x00A3F058>

Python Regular Expression Modifiers (Option Flags)

Following table contains the list of all Python Regular Expression or Python RegEx modifiers, along with their descriptions.

ModifierDescription
re.IPerforms case-insensitive matching
re.LInterprets words according to the current locale. This interpretation affects the alphabetic group (\w and \W), as well as the word boundary behavior (\b and \B)
re.MMakes $ match the end of a line (not just the end of the string) and makes ^ match the start of any line (not just the start of the string)
re.SMakes a period (dot) match any character, including a newline
re.UInterprets letters according to the Unicode character set. This flag affects the behavior of \w, \W, \b, and \B
re.XAllows ‘cuter’ regular expression syntax


This brings us to the end of this module about regular expression in python Tutorial. Here, we leant what Python 3 RegEx is, Regular Expression Characters in Python, Match Function of RegEx in Python, Special Sequence Characters of RegEx in Python, Search Function of RegEx in Python, also talked about Python Regex Modifiers. Now, if you want to know why python is the most preferred language for data science, you can go through this blog on Python for Data Science.

Further, check out our offers for Python training Course and also refer to the trending Python interview questions prepared by the industry experts.

Leave a Reply

Your email address will not be published. Required fields are marked *

Solve : *
9 + 27 =