loading...

Introduction To Regular Expression

shubham025 profile image Kumar Shubham ・1 min read
  • Commonly known as RegEx
  • RegEx is a string containing some special characters. These characters taken together forms a pattern.
  • This RegEx is used to check whether that particular pattern exists in any given string or not.

Python module re is imported to use Regular Expression methods

RegEx Functions

1.findall

  • It returns a list containing all matches or an empty list in case of no match.
  • It takes two parameter i.e, RegEx and String respectively.

2.Search

  • It Search the string for one match.
  • In case of match, it returns a MATCH OBJECT of only first match.
  • In Case of no match, it returns None.
  • It takes two parameter i.e,RegEx and String respectively.

3.Split

  • It returns a list where the string has been split at each match.
  • It takes 2-3 parameters.
  • First two parameters are RegEx and String respectively.
  • The Third parameter is an optional parameter,known as maxsplit parameter and takes int value as input.
  • This max split parameter controls maximum number of split can be performed on the String.

4.Sub

  • It looks for a match then replace it with new text string.
  • It takes 3-4 parameters,4th parameter is optional.
  • First 3 parameters are RegEx, newText and string respectively.
  • Fourth parameter is known as count parameter and takes int value as input.
  • This count parameter controls the maximum number of replacement performed on the string.

Posted on by:

shubham025 profile

Kumar Shubham

@shubham025

Pursuing Mca from Cusat

Discussion

markdown guide