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 Dictionary
Python dictionary is an unordered collection of items. While other compound datatypes have only value as an element, a dictionary has a key: value pair. Dictionaries are optimized to retrieve values when...

Black in Python
When you are upturn in your career from beginner to experienced in programming world, your team will start looking at ‘how you are writing?’ Here the responsibility piling up. Okay,...

Python- 5 reasons to learn python
1. Dynamically Typed You don’t have to declare a type when declaring a variable. It skips headaches of type casting JAVA:- int x = 1; x = (int) x/2;...


File Handling in Python - Basic Concept
File (Flat) Handling in Python Types of files in python: 1: Text file: Stores data in the form of characters. Customarily used to store text/string data. 2: Binary file: Stores data in the form of bytes....
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