Python re模块

1、匹配中文

# u'[\u4e00-\u9fa5]+'(匹配所有中文的unicode类型)
text='中文ABCD测试12345测试'
prog = re.compile(u'[\u4e00-\u9fa5]+\w+[\u4e00-\u9fa5]+')
res=prog.findall(text)
print(res)

2、分组匹配

# !/usr/bin/env python
# -*- coding:utf-8 -*-
# __author__ =

import re

text="""
terminal length 0
RuiJie#S6510-48VS8CQ-01#show vxlan mac local
Vxlan       MAC Address          Type     Location Interface                              Vlan   Live Time    
----------  -------------------- -------- -------- -------------------------------------- ------ -----------------   
   61009    4c00.0ab6.0301       DYNAMIC  LOCAL    AggregatePort 14                       1011   125d 23:07:34    
   61009    4c00.0ab6.0302       DYNAMIC  LOCAL    AggregatePort 16                       1011   125d 22:09:00     
   61011    f0ff.a9f8.70f3       DYNAMIC  LOCAL    AggregatePort 20                       1010   125d 23:07:34    
   61011    f0ff.a9f8.bac3       DYNAMIC  LOCAL    AggregatePort 21                       1010   125d 23:07:34    
   61011    f0ff.a9f8.bd65       DYNAMIC  LOCAL    AggregatePort 15                       1010   125d 21:36:57    
   61011    f0ff.a9f8.c2a1       MLAG     LOCAL    AggregatePort 16                       1010   127d 02:24:52    
   61011    f0ff.a9f8.c6ef       MLAG     LOCAL    AggregatePort 19                       1010   127d 02:13:05  
   61013    4c00.0ab6.0701       MLAG     LOCAL    AggregatePort 15                       1013   6d 15:21:45      
   61013    4c00.0ab6.0702       DYNAMIC  LOCAL    AggregatePort 15                       1013   125d 21:30:41    
   61013    4c00.0ab6.0703       DYNAMIC  LOCAL    AggregatePort 14                       1013   125d 23:07:34    
   61013    4c00.0ab6.0704       DYNAMIC  LOCAL    AggregatePort 16                       1013   6d 15:21:44      
   61013    4c00.0ab6.0705       DYNAMIC  LOCAL    AggregatePort 14                       1013   125d 23:07:34
   61014    4c00.0ab6.0a01       MLAG     LOCAL    AggregatePort 19                       1016   0d 00:06:48      
   61014    4c00.0ab6.0a02       DYNAMIC  LOCAL    AggregatePort 19                       1016   125d 23:07:34    
   61014    4c00.0ab6.0a03       MLAG     LOCAL    AggregatePort 19                       1016   0d 00:06:48      
   61014    4c00.0ab6.0a04       DYNAMIC  LOCAL    AggregatePort 19                       1016   0d 00:01:53      
   61014    4c00.0ab6.0a05       MLAG     LOCAL    AggregatePort 18                       1016   6d 15:26:36
RuiJie#S6510-48VS8CQ-01#
"""

pattern = re.compile(r'(?P<mac>[a-z0-9]+\.[a-z0-9]+\.[a-z0-9]+).*(?P<interface>AggregatePort\s+[0-9]+)')
res = pattern.finditer(text)
maclist = []
for i in res:
    """
    i.groupdict():把匹配的结果转换为字典
    """
    macdict = {}
    macdict['mac'] = i.groupdict()['mac']
    macdict['interface'] = i.groupdict()['interface'].replace('AggregatePort ', 'TFGigabitEthernet 0/')
    maclist.append(macdict)
print(maclist)

参考链接:
        https://www.cnblogs.com/huxi/archive/2010/07/04/1771073.html
        https://www.cnblogs.com/shenjianping/p/11647473.html
        https://docs.python.org/zh-cn/3.8/library/index.html     #Python 标准库
        https://docs.python.org/zh-cn/3.10/library/re.html
        https://www.cnblogs.com/Eva-J/articles/7228075.html#_label7

posted @ 2019-12-02 16:48  風£飛  阅读(98)  评论(0编辑  收藏  举报