What is regular expressions in python?

Description

To see what is regular expression and how to use it in python3.

Regular expressions

   special sequence of characters that helps
 you match or find other strings or
 sets of strings.

   The module re provides full support for
 regular expressions in python.

Regular expression match():

   This function attempts to match RE pattern
 to string with optional flags.

Regular expression search():

   This function searches for first occurrence 
of RE pattern within string with 
optional flags.

#Regular expression module

import re

#input string

string = “Python is a scripting language”;

#initialize match object

match = re.match( r’Pythons’, string, re.M|re.I)

if match:

print (“”, match.group())

else:

print (“No match”)

#initialize search object

search = re.search( r’Python’, string, re.M|re.I)

if search:

print (“Serach success “, search.group())

else:

print (“Nothing found”)

Leave Comment

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

clear formSubmit