This is a draft cheat sheet. It is a work in progress and is not finished yet.
dd
\d - stands for a digit character
\d\d\d-\d\d\d-\d\d\d\d or \d{3}-\d{3}-\d{4} 333-333-4444
Passing a string value representing your regular expression to re.compile() returns a Regex pattern object (or simply, a Regex object). To create a Regex object that matches the phone number pattern, enter:
>>>phoneNumRegex = re.compile(r'\d{3}-\d{3}-\d{4}') - by putting an r before the first quote of the string value, you can mark the string as a raw string, which does not escape characters.
>>>mo = phoneNumRegex.search('My number is 415-555-4242') >>>mo.group() '415-555-4242' |
A Regex object’s search() method searches the string it is passed for any matches to the regex. The search() method will return None if the regex pattern is not found in the string. If the pattern is found, the search() method returns a Match object. Match objects have a group() method that will return the actual matched text from the searched string.
Grouping with Parentheses
>>>phoneNumRegex = re.compile(r'(\d{3})-(\d{3}-\d{4})') >>>mo = phoneNumRegex.search('My number is 431-559-2243') >>>mo.group(1) '431' >>>mo.group(2) '559-2243' >>>mo.group(0) or >>>mo.group() '431-559-2243' >>>mo.groups() ('431', '559-2243')
>>>areaCode, mainNumber = mo.groups() >>>areaCode '431' |
If you need to match an actual parenthesis character, escape it: \(, \)
Matching Multiple Groups with the Pipe
>>>heroRegex = re.compile(r'Batman|Tina Fey') >>>heroRegex.search('Batman and Tina Fey.').group() 'Batman' >>>heroRegex.search('Tina Fey and Batman.').group() 'Tina Fey'
If you want to match any of the strings 'Batman', 'Batmobile', 'Batcopter', and 'Batbat':
>>>batRegex = re.compile(r'Bat(man|mobile|copter|bat)') >>>mo = batRegex.search('Batmobile lost a wheel') >>>mo.group() 'Batmobile' >>>mo.group(1) 'mobile' |
If you need to match an actual pipe character, escape it with a back- slash, like \|.
|