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...

Manage AWS ELB using Python Boto3 Automation

 Hey Automation Specialist! Welcome to Python-Orientation blog, In this article we are going to explore the possible functions what we can use for managing AWS ELB automations using Boto3 Python scripts.  AWS provides us 4 types of load balancers - CLB Classic Load balancer supports HTTP, HTTPS, TCP, TLS which is older and soon sun set. ALB Application Load Balancer which supports HTTP, HTTPS requests. Introduced in 2016 this V2 version Elastic Load Balancer. And in the 2017 AWS introduced Network Load Balancer NLB, it is designed to support Transport Layer protocols - TCP, UDP and TLS requests. And the latest one is more focused on Security Threat Monitoring - which lays on Network Layer and it support routing traffic to 3rd party Threat monitoring service which can be isolated from the Application services network. It works as Gateway for the external traffic so it is known as Gateway Load balancer. What is ELB? ELB automatically distributes the incoming network traffic (HTT...

Beginning of Python Automations using Boto3 for AWS

The objective of this post is for experimenting with AWS Boto3 automations and AWS Lambda, most of the realtime usecase on AWS cloud controlled and acceleration with them. We should know as SRE or DevOps Engineer how to refer to the Boto3 documentation where AWS team provided lot of details and examples of each Boto3 method, I'm pretty sure you could make great automations with this Boto3 module explore ideas. Python for AWS Using Boto3 Let's jump on it... Prerequisites You must have AWS account [this can be your company provided or free-tire account]. Getting Started Python Automations using Boto3  Step 1: Add User in IAM  Let's get into the IAM adding user you can provide username as devops-admin Select AWS Access TYPE {tic} programmatic access this allows to access key ID and secret access key for the AWS API, CLI SDK (Boto3 uses this). Set permissions for 'devopsuser' Select policy type as 'AdministrationAccess' to access AWS resources and s...