Quick Contact

     

    Python Regular Expressions

    Python regular expression is a special sequence of characters that helps to match a string or sets of strings based on a particular pattern.

    In Python, the module is known as “re” provides the support for regular expressions in Python. If any error occurs while compiling or handling a regular expression in Python, then this “re” module will raise an exception known as “re.error”.

    There are two important functions in the “re” module. They are “match” and “search” functions. In the following Python regular expression examples, we are going to use Raw Strings as “rexpression”.

    The match Function

    It is the function present in the “re” module that matches the RE pattern to string with optional flags.

    Syntax

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

    
    

    Following is the description of these parameters.

    Parameters Descriptions
    Pattern It accepts the regular expression that to be matched.
    String This is the string, which would be searched to match the pattern at the beginning.
    Flags This exception is increased by the sys.exit () function.

    The re.match function returns the matched object when the matching is successful and none when the matching fails. After that, we can use “group (num)” or “groups ()” function on matched object to get matched expression.

    Match Object Methods Descriptions
    group (num=0) This function returns the entire match or specific subgroup num.
    groups () This function returns all matching subgroups in a tuple. It will be null if there are not any.
    Regular expression example of the match function
    Python Regular Expressions

    When we execute the above Python program, we will observe the following output.

    Output
    Python Regular Expressions
    The search Function

    It is the function present in the “re” module that searches for the first occurrence of RE pattern within a string with optional flags.

    Syntax

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

    
    

    Following is the description of these parameters.

    Parameters Descriptions
    Pattern It accepts the regular expression that to be matched.
    String This is the string, which can be searched to linked the pattern anywhere.
    Flags This parameter is used to specify different flags using bitwise OR (|). These are the modifiers which are listed in the table below.

    The re.search function returns the matched object when the matching is successful and none when the matching fails. After that, we can use “group (num)” or “groups ()” function on matched object to get the matched expression.

    Match Object Methods Descriptions
    group (num=0) This function returns the entire match or specific subgroup num.
    groups () This function returns all matching subgroups in a tuple.
    Example of Search Function
    Python Regular Expressions
    Output
    Python Regular Expressions
    Search and Replace

    Python “re” module has an important function known as “sub”. This function is used to do a search and replace operations. Let’s understand this with the help of the following example.

    Syntax

    re.sub (pattern, replace, string, max=0)

    
    

    This “sub” method or function replaces all occurrences of the Regular Expression pattern present in the string with “replace” string parameter, it will substitute all of the occurrences unless max limit is passed in the parameter. This method will return a modified string after matching regular expression substitution with “replace” string parameter.

    Parameters Description
    Pattern It accepts the regular expression that to be matched.
    Replace It is the string which will replace or substitute the matching portion in the main String passed as a parameter.
    String This is the main string, which can be linked to match the pattern anywhere in the string.
    Max This is an optional argument that represents the limit for a maximum number of substitution with the matching pattern.
    Example
    Python Regular Expressions
    Output
    Python Regular Expressions
    Regular Expression Modifiers: Option Flags

    Regular expression literals include an optional modifier that controls various aspects of matching. These optional modifiers are specified as an optional flag. We can supply multiple modifiers by using exclusive OR (|) operation. Following are the representation for such an operation.

    Modifiers Descriptions
    re.I This modifier performs a case-insensitive matching.
    re.L This modifier interprets words according to the current locale. This type of interpretation affects the alphabetic group (\w and \W) as well as word boundary behavior (\b and \B).
    re.M This modifier makes $ match the end of a line and not just the end of the string. It makes ^ match the start of any line, and not just the beginning of the string.
    re.S This modifier is used to make a period (dot) match with any character, and it includes a newline as well.
    re.U This modifier interprets letters according to the Unicode character set, and this flag affects the behavior of \w, \W, \b, \B.

    Enroll Yourself: Python Training

    Copyright 1999- Ducat Creative, All rights reserved.