@zhangyu756897669
2017-09-07T12:58:44.000000Z
字数 3342
阅读 542
python官方文档
import re
phoneNumRegex = re.compile(r'\d\d\d-\d\d\d-\d\d\d\d')
mo = phoneNumRegex.search('Cell: 415-555-9999 Work: 212-555-0000')
mo.group()
'415-555-9999'
phoneNumRegex = re.compile(r'\d\d\d-\d\d\d-\d\d\d\d') # has no groups
phoneNumRegex.findall('Cell: 415-555-9999 Work: 212-555-0000')
['415-555-9999', '212-555-0000']
如果正则表达式中有组,那么findall()将返回一个元组列表。每个元组表示一个找到的匹配,它的项是正则表达式中每个组的匹配字符串。
phoneNumRegex = re.compile(r'(\d\d\d)-(\d\d\d)-(\d\d\d\d)') # has groups
phoneNumRegex.findall('Cell: 415-555-9999 Work: 212-555-0000')
[('415', '555', '9999'), ('212', '555', '0000')]
速记字符类 | 含义 |
---|---|
\d | 任意数字从0到9。 |
\D | 任何字符内不含0到9之间的数字 |
\w | 任何不是字母,数字或下划线字符的字符 |
\s | 任何空格,制表符或换行符 |
\S | 任何不是空格,制表符或换行符的字符 |
* 字符类对缩短正则表达式很好。字符类[0-5]将仅匹配数字0到5;这比打字(0 | 1 | 2 | 3 | 4 | 5)短得多。
xmasRegex = re.compile(r'\d+\s\w+')
xmasRegex.findall('12 drummers, 11 pipers, 10 lords, 9 ladies, 8 maids, 7swans, 6 geese, 5 rings, 4 birds, 3 hens, 2 doves, 1 partridge')
['12 drummers',
'11 pipers',
'10 lords',
'9 ladies',
'8 maids',
'6 geese',
'5 rings',
'4 birds',
'3 hens',
'2 doves',
'1 partridge']
vowelRegex = re.compile(r'[aeiouAEIOU]')
vowelRegex.findall('Robocop eats baby food. BABY FOOD.')
['o', 'o', 'o', 'e', 'a', 'a', 'o', 'o', 'A', 'O', 'O']
consonantRegex = re.compile(r'[^aeiouAEIOU]')
consonantRegex.findall('Robocop eats baby food. BABY FOOD.')
['R',
'b',
'c',
'p',
' ',
't',
's',
' ',
'b',
'b',
'y',
' ',
'f',
'd',
'.',
' ',
'B',
'B',
'Y',
' ',
'F',
'D',
'.']
beginsWithHello = re.compile(r'^Hello')
beginsWithHello.search('Hello world!')
<_sre.SRE_Match object; span=(0, 5), match='Hello'>
beginsWithHello.search('He said hello.') == None
True
endsWithNumber = re.compile(r'\d$')
endsWithNumber.search('Your number is 42')
<_sre.SRE_Match object; span=(16, 17), match='2'>
endsWithNumber.search('Your number is forty two.') == None
True
wholeStringIsNum = re.compile(r'^\d+$')
wholeStringIsNum.search('1234567890')
<_sre.SRE_Match object; span=(0, 10), match='1234567890'>
wholeStringIsNum.search('12345xyz67890') == None
True
wholeStringIsNum.search('12 34567890') == None
True
.
(点)字符称为通配符,并将匹配除换行之外的任何字符。
atRegex = re.compile(r'.at')
atRegex.findall('The cat in the hat sat on the flat mat.')
['cat', 'hat', 'sat', 'lat', 'mat']