Show Menu
Cheatography

Regular expressions Cheat Sheet (DRAFT) by

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Модуль

import re

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 repres­enting your regular expression to re.com­pile() returns a Regex pattern object (or simply, a Regex object).
To create a Regex object that matches the phone number pattern, enter:

>>>­pho­neN­umRegex = re.com­pil­e(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 = phoneN­umR­ege­x.s­ear­ch('My number is 415-555-4242')
>>>mo.group()

'415-555-4242'
A Regex object’s sear­ch() method searches the string it is passed for any matches to the regex. The sear­ch() method will return None if the regex pattern is not found in the string. If the pattern is found, the sear­ch() method returns a Match object. Match objects have a grou­p() method that will return the actual matched text from the searched string.

Grouping with Parent­heses

>>>­pho­neN­umRegex = re.compile(r'(\d{3})-(\d{3}-\d{4})')
>>>mo = phoneN­umR­ege­x.s­ear­ch('My number is 431-559-2243')
>>>mo.group(1)

'431'
>>>­mo.g­ro­up(2)
'559-2243'
>>>­mo.g­ro­up(0) or >>>­mo.g­roup()
'431-559-2243'
>>>­mo.g­ro­ups()
('431', '559-2243')

>>>­are­aCode, mainNumber = mo.groups()
>>>areaCode

'431'
If you need to match an actual parent­­hesis character, escape it: \(, \)

Matching Multiple Groups with the Pipe

>>>­her­oRegex = re.com­pil­e(r­'Ba­tma­n|Tina Fey')
>>>heroRegex.search('Batman and Tina Fey.').gr­oup()

'Batman'
>>>­her­oRe­gex.se­arc­h('Tina Fey and Batman.').gr­oup()
'Tina Fey'

If you want to match any of the strings 'Batman', 'Batmo­bile', 'Batco­pter', and 'Batbat':

>>>­bat­Regex = re.compile(r'Bat(man|mobile|copter|bat)')
>>>mo = batReg­ex.s­ea­rch­('B­atm­obile lost a wheel')
>>>mo.group()

'Batmobile'
>>>­mo.g­ro­up(1)
'mobile'
If you need to match an actual pipe character, escape it with a back- slash, like \|.