python 正规表达式匹配
#coding=utf-8
"""
user:
version:
function:
"""
def phoneNumRegex(content,re_str):
phone = re.compile(re_str)
mo = phone.search(content)
num = mo.group()
return num
import re
phoneNumRegex = re.compile(r'\d\d\d-\d\d\d-\d\d\d\d')
mo = phoneNumRegex.search('My mumber is 415-555-4242')
print('Phone number found: ' + mo.group())
#假定想要将区号从电话号码中分离,添加括号将在正则表达式中创建“分组”
phoneNumber = re.compile(r'(\d\d\d)-(\d\d\d-\d\d\d\d)')
mo = phoneNumber.search('My number is 415-555-4242.')
print(mo.group(1))
print(mo.group(2))
print(mo.group(0))
print(mo.group())
print(mo.groups())
# = mo.groups()返回多个值的元组,可以使用多重复制技巧,每一个值赋给一个独立的变量
areaCode,mainNumber = mo.groups()
print(areaCode)
print(mainNumber)
#用管道匹配多个分组
heroRegex = re.compile(r'Batman|Tina Fey')
mo1 = heroRegex.search('Batman and Tina Fey.')
print(mo1.group())
mo2 = heroRegex.search('Tina Fey and Batman.')
print(mo2.group())
#匹配以bat开头的任意一个
batRegex = re.compile(r'Bat(man|mobile|copter|bat)')
mo4 = batRegex.search('Batmobile lost a wheel')
print(mo4.group())
print(mo4.group(1))
#用问号实现可选匹配
batRegex = re.compile(r'Bat(wo)?man')
mo5 = batRegex.search('The Adventures of Batman')
print("mo5:" + mo5.group())
mo6 = batRegex.search('The Adventures of Batwoman')
print(mo6.group())
phoneRegex = re.compile(r'(\d\d\d-)?\d\d\d-\d\d\d\d')
mo7 = phoneRegex.search('My number is 415-555-4242')
print(mo7.group())
#匹配问号之前的分组0次或者1次
mo8 = phoneRegex.search('My number is 555-4242')
print(mo8.group())
#用星号匹配0次或多次
#用加号匹配一次或多次
#用花括号匹配特定的次数
#信心和非信心匹配
#字符分类 \d 0-9的任何数字 \D 除0-9以外的任何字符
#.字符为“通配符,匹配除换行之外的任意字符”
#用点-星匹配所有字符
import re def phoneNumRegex_zero(re_str,content): phone = re.compile(re_str) mo = phone.search(content) num = mo.group() return num def phoneNumRegex_one(re_str,content): phone = re.compile(re_str).search(content).group() return phone def phoneNumRegex_search(re_str,content): phone = re.search(re_str,content).group() return phone def phoneNumRegex_match(re_str,content): phone = re.match(re_str,content).group() return phone def phoneNumRegex_findall(re_str,content): phone = re.findall(re_str,content) return phone if __name__ == '__main__': content = '400-888-8888 is my number' content_str = 'my number is 400-800-1223' re_str = r'\d\d\d-\d\d\d-\d\d\d\d' print(phoneNumRegex_search(re_str,content_str)) #假定想要将区号从电话号码中分离,添加括号将在正则表达式中创建“分组” content_str = 'my number is 400-800-1223' re_str = r'(\d{3})-(\d{3}-\d{4})' print(phoneNumRegex_findall(re_str,content_str))
posted on 2020-10-29 19:48 sunny_2016 阅读(163) 评论(0) 编辑 收藏 举报