[关闭]
@zhangyu756897669 2017-09-07T12:58:44.000000Z 字数 3342 阅读 542

python

python官方文档


findall()方法

  1. import re
  2. phoneNumRegex = re.compile(r'\d\d\d-\d\d\d-\d\d\d\d')
  3. mo = phoneNumRegex.search('Cell: 415-555-9999 Work: 212-555-0000')
  4. mo.group()

'415-555-9999'

  1. phoneNumRegex = re.compile(r'\d\d\d-\d\d\d-\d\d\d\d') # has no groups
  2. phoneNumRegex.findall('Cell: 415-555-9999 Work: 212-555-0000')

['415-555-9999', '212-555-0000']

如果正则表达式中有组,那么findall()将返回一个元组列表。每个元组表示一个找到的匹配,它的项是正则表达式中每个组的匹配字符串。

  1. phoneNumRegex = re.compile(r'(\d\d\d)-(\d\d\d)-(\d\d\d\d)') # has groups
  2. 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)短得多。

  1. xmasRegex = re.compile(r'\d+\s\w+')
  2. 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']

制作你自己的字符类

  1. vowelRegex = re.compile(r'[aeiouAEIOU]')
  2. vowelRegex.findall('Robocop eats baby food. BABY FOOD.')

['o', 'o', 'o', 'e', 'a', 'a', 'o', 'o', 'A', 'O', 'O']

  1. consonantRegex = re.compile(r'[^aeiouAEIOU]')
  2. consonantRegex.findall('Robocop eats baby food. BABY FOOD.')

['R',
'b',
'c',
'p',
' ',
't',
's',
' ',
'b',
'b',
'y',
' ',
'f',
'd',
'.',
' ',
'B',
'B',
'Y',
' ',
'F',
'D',
'.']

插入符号和美元符号字符

  1. beginsWithHello = re.compile(r'^Hello')
  2. beginsWithHello.search('Hello world!')

<_sre.SRE_Match object; span=(0, 5), match='Hello'>

  1. beginsWithHello.search('He said hello.') == None

True

  1. endsWithNumber = re.compile(r'\d$')
  2. endsWithNumber.search('Your number is 42')

<_sre.SRE_Match object; span=(16, 17), match='2'>

  1. endsWithNumber.search('Your number is forty two.') == None

True

  1. wholeStringIsNum = re.compile(r'^\d+$')
  2. wholeStringIsNum.search('1234567890')

<_sre.SRE_Match object; span=(0, 10), match='1234567890'>

  1. wholeStringIsNum.search('12345xyz67890') == None

True

  1. wholeStringIsNum.search('12 34567890') == None

True

通配符

. (点)字符称为通配符,并将匹配除换行之外的任何字符。

  1. atRegex = re.compile(r'.at')
  2. atRegex.findall('The cat in the hat sat on the flat mat.')

['cat', 'hat', 'sat', 'lat', 'mat']

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注