It is used to match a pattern and extract data. For example, capture “Tweedle” in the string “Tweedle Bettle and Kettle”.

import re
text_from_router = 'vrf definition POLICE'
pattern = r'vrf\s+definition\s+(?P<vrf_name>\S+)'
match = re.search(pattern,text_from_router)
if match:
   print(match.groupdict())
# prints {'vrf_name': 'POLICE'} 
\s-->whitespace,\S+-->any character,(?P<name>)-->Create a named capturing group

Okay, it is time to parse the model ID string on IOS-XE and IOS-XR.

import re
def parse_model_ios(text):
    #cisco CSR1000v (VXE) processor (revision VXE) with 31K/32K memory.  
    model_regex = re.complie(r"Cisco\s+(?P<model>\S+)\s+\(\S+\)\s+prcessor\s+")
    model_match = model_regex.search(text)
    if model_match:
         return model_match.group("model")
    return None

def parse_model_iosxr(text):
    #Parse the model ID from the IOS-XR "show diag 0/0" command.
    #PID              : R-IOSXRV9000-RP-C,[]--->match literally space
    model_regex = re.compile(r"\s+PID\s+:[ \t]+(?P<model>\S+)")
    model_match = model_regex.search(text)
    if model_match:
        return model_match.group("model")
    return None 

Building structure data from IOS-XE and IOS-XR VRF configuration

#!/use/bin/env python
Author: Nick Russo
import re
def parse_rt_ios(text)
    vrf_list = ["vrf"+ s for s in text.strip().split("vrf") if s]