in

Python regular expressions and lambda functions – Day 11

In this article, you will learn python regular expression and python lambda function.

Python regular expression

New here?

Start learning Python from scratch for Free.

Many times a lot of data would be stored in files and we may have to pick and change only relevant portions from a file. Even though there are string functions that allow us to manipulate strings, when dealing with more complicated requirements, we would need more powerful tools.

Regular Expressions are used to check and extract relevant portions of a string based on a pattern and modify if required. 

Python has a module named ‘re‘ for regular expressions.

Two commonly used methods in the re module are search and sub. Search is used to find a pattern and sub is used to perform a substitution.

The requirement is to search for the pattern “Airlines” in the input_data.

Let’s understand how this can be achieved using re methods.

python regular expression

Two most prominently used methods in the re module are search and sub. Search is used to find a pattern and sub is used to perform a substitution.

Search method

CharacterDescriptionExample
[]A set of characters“[a-m]”
\Signals a special sequence (can also be used to escape special characters)“\d”
.Any character (except newline character)“he..o”
^Starts with“^hello”
$Ends with“world$”
*Zero or more occurrences“aix*”
+One or more occurrences“aix+”
{}Exactly the specified number of occurrences“al{2}”
|Either or“falls|stays”
()Capture and group  

Let’s see some examples of Search method:

  1. Checks if pattern “Air” is found in “Airline”
import re
if(re.search(r"Air","Airline")!=None):
    print("Pattern found")
else:
    print("Pattern not found")
  1. “.” stands for any character. If any two characters are there between A and l, then the pattern has matched
import re
if(re.search(r"A..l","Aopline")!=None):
    print("Pattern found")
else:
    print("Pattern not found")
  1. \d checks for a digit. If any digit is found between A and l, then the pattern is found
import re
if(re.search(r"A\dl","A2line")!=None):

4.[] does a single character substitution. We can specify a sequence of values. If any of the values are found, then the pattern has matched

import re

5.| acts like ‘or’ operator. If Hell or Fell is found in the string, the pattern is found

import re
if(re.search(r"Hell|Fell","Fellow")!=None):
    print("Pattern found")
else:  
    print("Pattern not found")

6.\s indicates a space. Here we are checking if there is a space after Air

import re
if(re.search(r"Air\s","Airline")!=None):
    print("Pattern found")
else:
    print("Pattern not found")

7.Checks if a number is found 0 or n times after A

import re
if(re.search(r"A\d*","A2234line")!=None):
    print("Pattern found")
else:
    print("Pattern not found")

8.{n} checks if the preceding character appears exactly n times. Here we are checking if there are 3 digits after A

import re
if(re.search(r"A\d{3}i","A223irline")!=None):
    print("Pattern found")
else:
    print("Pattern not found")

9.^ checks if a pattern is at the beginning of the string. Here we check if string begins with “A”

import re
if(re.search(r"^A","Airline")!=None):
    print("Pattern found")
else:
    print("Pattern not found")

Replacing Data( Subfunction )

  1. Replacing Flight with Plane
import re
flight_details="Flight Savana Airlines a2134"
print(re.sub(r"Flight",r"Plane",flight_details))
  1. To replace the ‘a’ to ‘A’ if it is followed by 4 numbers.
import re
flight_details="Flight Savana Airlines a2134"
print(re.sub(r"a(\d{4})",r"A\1",flight_details))

Python Lambda function

Lambdas are functions without names, in other words they are anonymous functions. They take inputs and return outputs but do not have a name. They are shortcuts to create simple temporary functions.

Lambdas are different from functions in the following respects:

  1. Functions have a name, Lambdas don’t.
  2. Functions have a return keyword, Lambdas don’t.
  3. Functions are used for reusability while lambdas are used inline whenever needed.
  4. Functions can have multiple statements, Lambdas can have only an expression.

Lambdas are similar to functions in the following respects:

  1. Both can take parameters and return a value.

Thankyou for reading out the full article, if you find this series useful please share it with your friends and relatives.

some related concepts

More about lambda function

What do you think?

Written by admin

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

GIPHY App Key not set. Please check settings

Loading…

0

List of Article Submission Sites to get Do-Follow backlinks

Post Anatomy: The perfect blog posts writing guide in 2020