UrbanPro
true

Learn Python Training from the Best Tutors

  • Affordable fees
  • 1-1 or Group class
  • Flexible Timings
  • Verified Tutors

Search in

Using Lambda and Filter with Regular expressions.

Michael Devine
17/10/2018 0 0

1) Start the Python interpreter and import the re module:

>>> import re

 

2)Define a tuple strings to filter with regular expressions:

t=('python','pycon','perl','peril','step','stop','come','came','exercise','hello','storm','my.mail','year','yam')

 

3) The 'Lambda' will execute the 'match' on each element of the tuple. A match matches only if found at the beginning. If the return of the 'match' is 'true' is collected in the 'Filter' object.

>>> a=filter(lambda i:re.match(r'py',i),t)
>>> print(*a)
python pycon
>>>

>>> 

>>> a=filter(lambda i:re.match(r'st',i),t)
>>> print(*a)
step stop
>>>

>>> a=filter(lambda i:re.match(r'me',i),t) 
>>> a
<filter object at 0x044203F0>

>>> list(a)
[]
>>> print(*a)

 

>>> 

4) To find strings anywhere in the input, use re.search:

>>> a=filter(lambda i:re.search(r'me',i),t)
>>> print(*a)
come came
>>> a=filter(lambda i:re.search(r'o',i),t) 
>>> print(*a)
python pycon stop come hello
>>>

 5) Use for the period (dot) for 'any character':

>>> a=filter(lambda i:re.search(r'c.m',i),t)
>>> print(*a)
come came
>>>

>>> a=filter(lambda i:re.search(r'st.',i),t)
>>> print(*a)
step stop
>>>

>>> a=filter(lambda i:re.search(r'y.m',i),t)
>>> print(*a)
my.mail yam
>>>

 

6) match only the period (by escaping the special character):

>>> a=filter(lambda i:re.search(r'y\.m',i),t)
>>> print(*a)
my.mail
>>>

 

7)  search for any number of repeating characters by using the asterisk, which can match a series of whatever character is in front of it:

>>> a=filter(lambda i:re.search(r'y.*',i),t)
>>>
>>> print(*a)
python pycon my.mail year yam
>>>

 

8) The * matches zero or more occurrences of a character between two z’s. The + matches at least one or more characters:

>>> a=filter(lambda i:re.search(r'l.*',i),t)
>>> print(*a)
perl peril hello my.mail
>>> a=filter(lambda i:re.search(r'l.*l',i),t)
>>> print(*a)
hello
>>> a=filter(lambda i:re.search(r'l.+l',i),t)
>>> print(*a)

>>>

 

9) Now you know how to match anything with, say, a ‘ c ’ in it:

>>> a=filter(lambda i:re.search(r'c+',i),t)
>>> print(*a)
pycon come came exercise
>>>

 

10) How would you match anything without a ‘ c ’ ? Regular expressions use square brackets to denote special sets of characters to match, and if there’s a caret at the beginning of the list, it means any string that has a character that isn’t the specified character in the set, so:

>>> a=filter(lambda i:re.search(r'[^c]',i),t)
>>> print(*a)
python pycon perl peril step stop come came exercise hello storm my.mail year yam
>>> a=filter(lambda i:re.search(r'[^c]*',i),t)
>>> print(*a)
python pycon perl peril step stop come came exercise hello storm my.mail year yam
>>> a=filter(lambda i:re.search(r'[^c]+',i),t)
>>> print(*a)
python pycon perl peril step stop come came exercise hello storm my.mail year yam

 

11) To match any string without a ‘ c ’ in it, you have to use the ^ and $ special characters to refer to the beginning and end of the string and then tell re that you want strings composed only of non - c characters from beginning to end:

>>> a=filter(lambda i:re.search(r'^[^c]*$',i),t)
>>> print(*a)
python perl peril step stop hello storm my.mail year yam
>>>

0 Dislike
Follow 1

Please Enter a comment

Submit

Other Lessons for You

Bit-wise operators
Operation Syntax Function Bitwise And a & b and_(a, b) Bitwise Exclusive Or a ^ b xor(a, b) Bitwise Inversion ~ a invert(a) Bitwise Or a b or_(a, b) Left...

Python : functional programming characteristics
Functions are first class (objects). That is, everything you can do with "data" can be done with functions themselves (such as passing a function to another function). Recursion is used as a primary...

Topics covered in Python Basics course
Chapter 1: Introduction Introduction to Python Setting up path Chapter 2: Variable Integer String Operators (Arithmetic, logical, relational) Chapter 3: Data Structure Tuple List Dictionary ...

A program to determine the molecular weight of a hydrocarbon based on the number of Hydrogen,Carbon and Oxygen atoms.
import osimport math# A program to calculate Molecular Weight of a Hydrocarbon # These are the atomic weights(grams/mole) of Hydrogen,Carbon and OxygenatomicwtH = 1.0079atomicwtC = 12.011atomicwtO = 15.994Hatoms...

Python/Dejango Training
Python/Django Training Content Duration: 2.5 months 1. Introduction to Python Python - The Universal Language 2. Getting Started Installing Python Python - *Hello World* Using the Interpreter Python...
X

Looking for Python Training Classes?

The best tutors for Python Training Classes are on UrbanPro

  • Select the best Tutor
  • Book & Attend a Free Demo
  • Pay and start Learning

Learn Python Training with the Best Tutors

The best Tutors for Python Training Classes are on UrbanPro

This website uses cookies

We use cookies to improve user experience. Choose what cookies you allow us to use. You can read more about our Cookie Policy in our Privacy Policy

Accept All
Decline All

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 55 lakh students rely on UrbanPro.com, to fulfill their learning requirements across 1,000+ categories. Using UrbanPro.com, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 7.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on UrbanPro.com. Whether you are looking for a tutor to learn mathematics, a German language trainer to brush up your German language skills or an institute to upgrade your IT skills, we have got the best selection of Tutors and Training Institutes for you. Read more