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
>>>