Ticker

6/recent/ticker-posts

Python: Regular Expressions

If we want to represent a group of Strings according to a particular format/pattern then we should go for Regular Expressions.

i.e Regualr Expressions is a declarative mechanism to represent a group of Strings accroding to particular format/pattern.

Eg 1: We can write a regular expression to represent all mobile numbers

Eg 2: We can write a regular expression to represent all mail ids.

The main important application areas of Regular Expressions are

  1.  To develop validation frameworks/validation logic
  2.  To develop Pattern matching applications (ctrl-f in windows, grep in UNIX etc)
  3.  To develop Translators like compilers, interpreters etc
  4.  To develop digital circuits
  5.  To develop communication protocols like TCP/IP, UDP etc.

We can develop Regular Expression Based applications by using python module: re

This module contains several inbuilt functions to use Regular Expressions very easily in our applications.

1. compile()

re module contains compile() function to compile a pattern into RegexObject.

pattern = re.compile("ab")

2. finditer():

Returns an Iterator object which yields Match object for every Match

matcher = pattern.finditer("abaababa")

On Match object we can call the following methods.

  1. start() => Returns start index of the match
  2. end() => Returns end+1 index of the match
  3. group() => Returns the matched string

Eg:

1) import re count=0 
2) pattern=re.compile("ab") 
3) matcher=pattern.finditer("abaababa") 
4) for match in matcher: 
5) count+=1 
6) print(match.start(),"...",match.end(),"...",match.group()) 
7) print("The number of occurrences: ",count) 
Output:
0 ... 2 ... ab
3 ... 5 ... ab
5 ... 7 ... ab
The number of occurrences: 3

Pre defined Character classes:

\s => Space character

\S => Any character except space character

\d => Any digit from 0 to 9

\D => Any character except digit

\w => Any word character [a-zA-Z0-9]

\W => Any character except word character (Special Characters)

. => Any character including special characters

Qunatifiers:

We can use quantifiers to specify the number of occurrences to match.

a => Exactly one 'a'

a+ => Atleast one 'a'

a* => Any number of a's including zero number

a? => Atmost one 'a' ie either zero number or one number

a{m} => Exactly m number of a's

a{m,n} => Minimum m number of a's and Maximum n number of a's

"Python Regular Expression *"

"Python Regular Expression Online"

"Python Regular Expression Extract String"

"Python Regular Expression Match Example"

"Python Regular Expression Split"

"Python Regular Expression Generator"

"Python Regular Expression Module"

"Real Python Regular Expressions"

"Python Regular Expressions"

"Python3 Regular Expressions"