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

Python Overview
Python is a high-level, interpreted, interactive and object-oriented scripting language. Python was developed by Gudo vanRossum in the late eighties and early nineties at the National Research Institute...

Using the random module: another program.
A Python module is just a file that contains reusable code like functions. If a program is going to use functions that are stored in another module, it needs to first import that module.The random module...

Python Programming
Python is a widely used general-purpose, high-level programming language. This is an Open Source Software and its source code is available with a license in which the Copyright holder provides the rights...

Assignment operators
Operation Syntax Comment Assignment = Control flow right to left Add AND += Incremental Subtract AND -= Decremental Multiply AND *= Multiplicative increment Division...

Back Reference In Python Regex
What if your interviewer asks about this? We know Regex have some useful functions like: match ( Ex: re.match(pattern,dataSource) ) search ( Ex: re.search(pattern,dataSource) ) sub ...
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

Book a Free Demo

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