Methods and Flags in Regular Expressions

In this class, We discuss Methods and Flags in Regular Expressions.

For Complete YouTube Video: Click Here

The reader should have prior knowledge of regular expressions and special sequences. Click here.

Methods and Flags

M Flag

Take an example and understand the M flag.

The below example shows the program using the flag ‘M’.

# re.M multiline
import re
string="""hello how are you
hello howz going on
every thing good """
x=re.findall(r'^hello',string,re.M)
print(x)

Output:
['hello', 'hello']

In the example, we have a multi-line string.

When we use the symbol’ ^’. the expression finds the match at the beginning of the line.

In the multi-line string, we use the flag’ M’ to check the matching expression starting of each line.

The above output shows the matching expression starting of each line.

the below example shows the program to match the expression at the end of each line.

# re.M multiline
string="""hello how are you
hello howz going on
every thing good going on"""
x=re.findall(r'on$',string,re.M)
print(x)

Output:
['on', 'on']

I Flag

The flag ‘I’ used to ignore the case sensitivity.

We need to identify words without considering the case most of the time.

The below example shows the program ignoring the case.

# re.I ignore casesensitive
string="""hello how are you
Hello howz going on
every thing good going on"""
x=re.findall(r'^hello',string,re.M|re.I)
print(x)

Output:
['hello', 'Hello']

S Flag

The dot symbol considers a character. Default dot symbols do not consider a new line.

To view a new line when we use the dot symbol. the flag ‘s’ is used.

The below example shows a program considering a new line as a character.

string="""hello how are you
Hello howz going on
every thing good going on"""
x=re.findall(r'n.',string,re.M|re.I|re.S)
print(x)

Output:
['ng', 'n\n', 'ng', 'ng']

X Flag

The flag ‘X’ used to mention comments in the regular expressions.

The below example shows the program using comments in the regular expression.

# re.X comments in expression
string="""hello how are you
i am giving 55.56 rupees"""
x=re.findall(r"""\d+ #integral part
               \. #decimal part
               \d* #floating part""",string,re.X)
print(x)

Output:
['55.56']

findall Function

The function findall identifies the matching expression and returns the list of matchings.

We used this Function many times in our previous class.

search Function

The search function identifies the matching expression.

More than one match found returns the first matching object.

The span method in the returned object displays the index values of the matching object.

The start method in the returned object display the starting index of the matching object

Similarly, the end method displays the end index of the matching object.

If no matches, the search function returns none.

The below example shows the program using the search function.

string="hello how are you hello"
x=re.search(r'hello',string)
print(x.span())
print(x.start())
print(x.end())

Output:
(0, 5)
0
5

The search function works on multi-line strings.

The below example shows the program using the multi-line input.

string="""hello how are you hello
          hello mike"""
x=re.search(r'mike',string)# search in multiline also
print(x.span())

Output:
(39, 43)

split Function

The split function splits the string whenever the Function finds a matching expression.

split Function is used on multi-line input.

The below example shows the program using the split function.

#split function split where matching return list
string="""hello how are you hello
          hello mike"""
x=re.split(r'mike',string)# search in multiline also
print(x)

Output:
['hello how are you hello\n          hello ', '']

We can mention the maximum splits in the split function.

The below example shows the program using maximum splits.

string="""hello how are you hello
          hello mike"""
x=re.split(r'\s',string,2)# maximum splits
print(x)

Output:
['hello', 'how', 'are you hello\n          hello mike']

sub Function

The function ‘sub’ will substitute the required string where ever matching occurs.

The below example shows the program substitute hash in place of space.

string="""hello how are you hello
          hello mike"""
x=re.sub(r'\s','#',string)# substitute in place matching found
print(x)

Output:
hello#how#are#you#hello###########hello#mike

group Function

Suppose we write two regular expressions. the two expressions are separated using ().

If the user wants to identify the matching expression for the first regular expression?

We use the function ‘group’ on the matching object.

The below example shows the program to use the method ‘group’ on the matching object.

#group functions of match object
string="hello how are you hello"
x=re.search(r'(.*) (.*)',string)# substitute in place matching found
print(x.group(1))
print(x.group(2))

Output:
hello how are you
hello

In the above example, the first regular expression taking the maximum possible match.

The first expression is written to identify zero or any number of characters.

After the first expression matches immediately, the second expression should match.

The second expression identifies the sequence of characters.

There are many possibilities to be considered in the above regular expression.

Example: ‘hello’ can be matched for the first expression, and ‘how are you hello’ can be matched for the second expression.

The other possibility, ‘hello how’ can be matched to the first expression, and ‘are you hello’ can be matched to the second expression.

Point to understand: Python chose the maximum possible for the first expression.