Skip to main content

Regular Expressions in Python



This session about Python Regular expressions how we can work with patterns and the specific methods available in re module.

  1. match()
  2. group(), groups()
  3. search()
  4. compile()
  5. sub()

Regular expression sample in Python

Here is the sample for match function use.

#!/usr/bin/python
"""
This program illustrates the usage of match, group functions in re module
This also shows how to use re flags
"""
import re

line = "Python Orientation course helps professionals fish best opportunities"

m = re.match( r'(.*) helps (.*?) .*', line, re.I)

if m:
   print "m.group() : ", m.group()
   print "m.group(1) : ", m.group(1)
   print "m.group(2) : ", m.group(2)
else:
   print "Don't have match!!"
The output
>>> execfile('c:/pybin/rematch.py')
m.group() :  Python Orientation course helps professionals fish best opportunities
m.group(1) :  Python Orientation course
m.group(2) :  professionals

=======

Remove, Replace using re

Example for remove, replace the pattern in the given string
#!/usr/bin/python
import re

DOB = "07-11-1973# This is DOB "

# Delete Python-style comments
num = re.sub(r'#.*$', "", DOB)
print "DOB Num : ", num

# Remove anything other than digits
x = re.sub(r'\D', "", DOB)    
print "DOB without - : ", x


# Substituting other symbol in anything other than digits
FDOB = re.sub(r'\D', "/", num)    
print "DOB in new format : ", FDOB

=======

Searching Pattern

Example for Searching the pattern in a given string

""" This program illustrates is 
 one of the regular expression method i.e., search()"""
import re 
# importing Regular Expression built-in module
text = 'This is my First Regulr Expression Program'
patterns = [ 'first', 'that‘, 'program' ]
# In the text input which patterns you have to search

for pattern in patterns:
    print 'Looking for "%s" in "%s" ->' % (pattern, text),
    if re.search(pattern, text,re.I):
        print 'found a match!'
 # if given pattern found in the text then execute the 'if' condition
    else:
        print 'no match'
 # if pattern not found in the text then execute the 'else' condition
Python Regular Expression

Comments

Popular posts from this blog

AWS Auto Scaling using Python Boto3

Auto Scaling Group in AWS configure using Python Boto3  What is Auto Scaling means?  This is key capability or power of Cloud Computing Engineers believe in their skills on scaling abilities.  Amazon EC2 Auto Scaling helps to maintain application availability and lets automatically add or remove EC2 instances using scaling policies that we define.  There are 2 types of scaling policies : Dynamic or predictive. These scaling policies let us add or remove EC2 instances capacity to service established or real-time demand patterns. It contains various steps involved in Auto Scaling process using Python Boto3 we will explore every step that accumulate to form a complete automation solution for a DevOps project. ASG Groups associated with ELB and EC2 instances   Understanding AWS Auto scaling configuration steps Check any running instances Create launch configuration Configure ASG for Auto scaling Verify the configuration Disable Auto Scaling In order to setup A...

Python Interview Questions -Coding Snipets

 This post is dedicated for all DevOps Engineer, Software Engineers who are preparing for Coding Interviews,  Overview of Coding Interviews Most Companies looking for People with minimum Coding knowledge. In a coding interview, you will be given a small problem to solve within 10 - 20 minutes online screen or in-person on their system. In the question, you might be having some part of the code framed and you might be asked to write a snippet of code in between. You need to understand the code comments and proceed to build the expected snippet of code. Bigger companies look for the General purpose questions, where small companies look for specific questions. The General questions would be like this: Determine if the given word is a palindrome or not. (Example madam) Determine given number is prime or not. How to prepare for a coding interview? Now we have the flexibility to choose the programming language on which you are comfortable.  In general DevOps Infra guys will be ...

Program Controls and loops in Python

Python Programming controls The flow of program will be controlled with conditional statements where this flow  if condition if-else condition if-elif condition while loop while - else loop for loop for with if- else loop if condition Here I would like to work on program control statements, wherein relate some operating system functionalities so that, this would give some basic prototype for the sys admin scripts. #!/usr/bin/python # This illustrate if condition # Filename : ifex.py import os d=os.listdir('/home/pavanbsd/pybin') print d f='ifex.py' if f in d: print "File exists" else: print "Not found..." if-elif-else ladder No worries, coming soon... When you need the task that need to be done repeatedly then your choice is using loops. Python provides two simple loop constructs easy to use! More powerful then other scripting compare to, it has 'else' block. while loop Understand the power of while loop,...