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.
- Introduction to Programming – Day1
- Introduction to Algorithms – Day 2
- Python operators and variables – Day 3
- Python Control Structures – Day 4
- Functions in Python – Day 5 of Python Series
- How to setup Eclipse IDE for Python? – 6th Day of Python Series
- Collection Data types in Python – 7th Day of Python Series
- Python Debugging and Testing –Python Series-8th day
- Python Built-in Functions, Libraries, and modules – Day 9
- Python regular expressions and lambda functions – Day 10
- Introduction of Python Object-oriented programming(Python Oops) – Day 11
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.
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
Character | Description | Example | |
[] | 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:
- 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")
- “.” 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")
- \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 )
- Replacing Flight with Plane
import re flight_details="Flight Savana Airlines a2134" print(re.sub(r"Flight",r"Plane",flight_details))
- 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:
- Functions have a name, Lambdas don’t.
- Functions have a return keyword, Lambdas don’t.
- Functions are used for reusability while lambdas are used inline whenever needed.
- Functions can have multiple statements, Lambdas can have only an expression.
Lambdas are similar to functions in the following respects:
- 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
Comments
Loading…