Free tech learning for everyone, from basics to pro

Network AutomationProgramming and Application SecurityIntermediate

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.

Using a regular expression on show outputRun commandshow ipinterface briefGet textone stringApply patternre.findallGet groupsname, IP,statusUse the datareport orcheck
Figure 1. Regular expressions turn unstructured CLI text into data your script can use.

The most useful pattern pieces

PatternMatches
\dA 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()?
search() returns the first match (or None); findall() returns every match as a list.
What does a capture group do?
It marks the part of the pattern whose matched text you want to extract.
Why write regex patterns as raw strings in Python?
So backslashes such as \d are passed to the re module unchanged instead of being treated as escape characters.

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.
Tracks your learning on this device only. See your progress on the learning paths.
Verified author✓ Valid
Issued to
Mohamed Fayas
Role
Founder of FM TecHub
Credentials
CCNABTEC HND Network EngineeringBEng (Hons) Computer Networking & Cloud Security
Issuer
FM TecHub · fmtechub.com

He writes practical, plain-English guides on networking, security and cloud.

More about the author →

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.

Email updates: coming soon

New notes, tools and security news from FM TecHub by email, explained simply. Free. We're not collecting email addresses yet; see our privacy policy.

PortServiceStatus
SMTP/25Email updatesopening soon

Meanwhile, stay connected: