Regular Expressions

 Regular expressions

--------------------

These are mainly used for pattern matching.


These are used for validating data based on the 

given patterns.


A regular expression is a group of special characters which is used for matching a pattern.


For example, \d is used for matching numbers(0-9)



To use regular expression, we import the module re.


And it has the following functions:


1.match(pattern,data,flags):

  Matches the given pattern at the begining on the given data.

  If it matches , it will return the matched object

otherwise it will return None.


2.search(pattern,data,flags):

  This function matches the pattern on the given data at begining or at ending or at middle.

  If it matches , it will return the matched object

otherwise it will return None.


3.group():

   Is used for returning matched data from matching 

object


Example

=======

import re


result= re.match("Apple","Apple Computers ")


if result:

    print("Matched data is ",result.group())

else:

    print("No match")




Example2

--------

import re


result= re.search("Apple","PineApple Computers ")


if result:

    print("Matched data is ",result.group())

else:

    print("No match")



Example3

--------

re.I is a flag, used for ignoring case.


import re


result= 

,  re.search("Apple","Pineapple Computers ",re.I)


if result:

    print("Matched data is ",result.group())

else:

    print("No match")





character        Description
-----------------------------------
.               It represents any char except
                   \n


^               Match at the begining
 

$               Match at the end.


?               Match 0 or 1 time

+               Match 1 or more time

*               Match 0 or more times.

[]              Match either of char in the                 character set

[^]             Negation
                Match no char in the character set

()              Match all chars in the charcter set


|               pipe


\A              Match at the begining

\Z              Match at the end

\d              match a digit(0-9)

\D              An opposite of \d

\w              Match a word(a-z,A-Z,0-9, _)

\W              An opposite of \w

\s              Match a space char.

\S              An opposite of \s

\\              Match \


Example

import re
#result=re.search(".","#")
#result=re.search("^A","Apple")
#result=re.search("e$","Apple")

#result=re.search("fo?d","fod")

#result=re.search("fo+d","food")

#result=re.search("fo*d","fd")

#result=re.search("f[aeiou]d","fad")

#result=re.search("f[^aeiou]d","fzd")

#result=re.search("f(aeiou)d","faeioud")

#result=re.search("^A|e$","PineApple")

#result=re.search("\AA","Apple")

#result=re.search("e\Z","Apple")

#result=re.search("\d","9")

#result=re.search("\D","A")

#result=re.search("\w","A")

#result=re.search("\W","$")

#result=re.search("\s"," ")

#result=re.search("\S","*")

result=re.search(r"\\",r"\\")


if result:
    print("Matched data ",result.group())
else:
    print("No match")


    
#Write a programme to match a series of digits

import re

result=re.search("\d+","1223423234")
if result:
    print("Matched data ",result.group())
else:
    print("No match")



#Write a programme to validate a 10 digit mobile number
#And start digit must be one of 9,8,7,6

import re

result=re.search("^[9876]\d{9}","9848098480")
if result:
    print("Matched data ",result.group())
else:
    print("No match")
    



#Write  a  regular expression to match a .

import re

result=re.search(r"\.","\.")
if result:
    print("Matched data ",result.group())
else:
    print("No match")



Comments

Popular posts from this blog

Solving maze puzzle in python