Regular expressions for network engineers: parsing show output with Python
In short: Regular expressions are patterns that find and extract text. In Python, the re module's search() finds the first match and findall() returns every match, and capture groups pull out pieces such as interface names and IP addresses. Regex is great for small parsing jobs, but for complex output use structured data or parsers such as TextFSM or Genie.
Why network engineers need regex
Many devices still return plain text from show commands. To use that output in a script, such as finding interfaces that are down or collecting IP addresses, you must pull out the parts you need. Regular expressions (regex) are patterns that describe text, and Python's built-in re module uses them. Commands can be collected with tools like Netmiko.
The most useful pattern pieces
| Pattern | Matches |
|---|---|
\d | A digit; \d+ means one or more digits |
\S+ | One or more non-space characters, such as an interface name |
\s+ | One or more spaces or tabs, useful between columns |
. and \. | Any character; an escaped dot means a real dot |
( ) | A capture group: the part you want to keep |
^ and $ | Start and end of a line (with the MULTILINE flag) |
Parsing interfaces with findall
import re
output = """Interface IP-Address OK? Method Status Protocol
GigabitEthernet0/0 10.0.0.1 YES manual up up
GigabitEthernet0/1 unassigned YES unset administratively down down
Loopback0 192.168.255.1 YES manual up up"""
pattern = r"^(\S+)\s+(\d+\.\d+\.\d+\.\d+|unassigned)\s+\S+\s+\S+\s+(up|down|administratively down)\s+(up|down)$"
for name, ip, status, proto in re.findall(pattern, output, re.MULTILINE):
print(f"{name:20} {ip:15} {status}/{proto}")
Each pair of brackets is a capture group, so findall() returns a tuple for each matching line. Always write patterns as raw strings (r"...") so backslashes are passed to re unchanged. To find just the first match, use re.search() and read groups with match.group(1).
Regex or a proper parser?
Regex is perfect for quick checks and small jobs, such as finding the software version or a serial number. It becomes fragile when output changes between versions or spans many lines. For larger jobs, prefer:
- Structured output from APIs, NETCONF or RESTCONF, which returns JSON or XML instead of text (see RESTCONF).
- Ready-made parsers such as TextFSM templates and Genie, which already handle many commands (see parsing CLI output).
Test patterns on real output, including unusual lines, before trusting them. Online regex testers help, but never paste real configurations containing secrets into public websites.
Real-world example: finding down interfaces at 50 sites
A fictional retail chain's network engineer needs a morning report of interfaces that are administratively up but have their line protocol down, across 50 branch routers. She collects show ip interface brief from each router and uses one regex with capture groups to extract name, status and protocol. The script lists only interfaces with status "up" and protocol "down" and emails the result. On day one it spots a failed backup link at three branches that nobody had noticed. Later, the team moves to Genie parsers for more complex commands but keeps the simple regex for this report.
Practice
What is the difference between re.search() and re.findall()?
What does a capture group do?
Why write regex patterns as raw strings in Python?
Key takeaways
- Regex extracts useful data from plain-text show output.
- Learn a few core pieces: \d, \S, \s, escaped dots and capture groups.
- Use re.findall() for all matches and re.search() for the first.
- Always use raw strings for patterns.
- For complex output, prefer APIs or parsers such as TextFSM and Genie.
Original content written by Mohamed Fayas for FM TecHub. © 2026 FM TecHub. You're welcome to share a link to this page; please don't copy the text elsewhere without permission.