基于Python 2.7
#!/usr/bin/env python
# -*- encoding=utf-8 -*-
import re
s = 'ABC\\-001' #对应 'ABC\-001'
s = r'ABC\-001' #加r前缀,就不用考虑转义的问题了。
print re.match('^\d{3}\-\d{3,8}$','010-12345')
# 匹配成功返回一个Match对象,否则返回None.
#re.match()的常见写法
'''
test = '输入字符串'
if re.match('正则表达式', test):
print 'ok'
else:
print 'failed'
'''
#re.split()函数的使用
print re.split('\s+','a b c')
# ['a', 'b', 'c']
print re.split('[\s\,]+','a,b, c d')
#['a', 'b', 'c', 'd']
print re.split('[\s\,\;]+','a,b;; c d')
#['a', 'b', 'c', 'd']
#提取子串,用()表示要提取的分组
m = re.match('^(\d{3})-(\d{3,8})$','010-12345')
print m.group(0) #原始字符串
print m.group(1) #第一个子串
print m.group(2) #第二个子串