import re
#Sample Msg
sample="This is roger. my contact no is 415-555-4242,& 415-555-4243, 416-555-4244"
phoneNumRegex = re.compile(r'\d\d\d-\d\d\d-\d\d\d\d')
#Search() return first match
mob=phoneNumRegex.search(sample)
print mob.group()
415-555-4242
mob=phoneNumRegex.findall(sample)
#find all return All matches
mob=phoneNumRegex.findall(sample)
print mob
['415-555-4242', '415-555-4243', '416-555-4244']
let say we want to categrise our result , like above phone number consist of area code & actual number . using group we can find it & use it easily.
phoneNumRegex = re.compile(r'(\d\d\d)-(\d\d\d-\d\d\d\d)')
mob=phoneNumRegex.findall(sample)
print mob
#for find all our result is set of tuple & each touple consist of 2 group based on our regression
[('415', '555-4242'), ('415', '555-4243'), ('416', '555-4244')]
mo=phoneNumRegex.search(sample)
print 'area code: ',mo.group(1)
print 'phone no :',mo.group(2)
area code: 415 phone no : 555-4242
The regular expression r'Batman|Tina Fey' will match either 'Batman' or 'Tina Fey' . When both Batman and Tina Fey occur in the searched string, the first occurrence of matching text will be returned as the Match object.
heroRegex = re.compile (r'Batman|Tina Fey')
mo1 = heroRegex.search('Batman and Tina Fey.')
print mo1.group()
Batman
mo2 = heroRegex.search('Tina Fey and Batman.')
print mo2.group()
Tina Fey
mo3=heroRegex.findall('Tina Fey and Batman.')
print mo3
['Tina Fey', 'Batman']
batRegex = re.compile(r'Bat(man|mobile|copter|bat)')
mo = batRegex.search('Batmobile lost a wheel')
print mo.group()
Batmobile
mo = batRegex.search('Batbat lost a wheel')
print mo.group()
Batbat
batRegex = re.compile(r'Bat(wo)?man')
#here "wo" is optional
mo1 = batRegex.search('The Adventures of Batman')
print mo1.group()
Batman
mo1 = batRegex.search('The Adventures of Batwoman')
print mo1.group()
Batwoman
The * (called the star or asterisk) means “match zero or more”—the group that precedes the star can occur any number of times in the text
batRegex = re.compile(r'Bat(wo)*man')
mo1 = batRegex.search('The Adventures of Batman')
print mo1.group()
Batman
mo1 = batRegex.search('The Adventures of Batwoman')
print mo1.group()
Batwoman
mo1 = batRegex.search('The Adventures of Batwowowoman')
print mo1.group()
Batwowowoman
Regex (Ha){3} will match the string 'HaHaHa <br > egex (Ha){3,5} will match 'HaHaHa' , 'HaHaHaHa' , and 'HaHaHaHaHa' .
haRegex = re.compile(r'(Ha){3}')
mo1 = haRegex.search('HaHaHa')
print mo1.group()
HaHaHa
haRegex = re.compile(r'(Ha){3,5}')
mo1 = haRegex.search('HaHaHaHa')
print mo1.group()
HaHaHaHa