Real-World Regex Applications
Regex isn’t just for log parsing. These patterns apply to every aspect of your digital life.
Professional Work
ISE & 802.1X Authentication
| Pattern | Regex (ERE) | Use Case |
|---|---|---|
Session ID |
|
Extract session identifiers |
MAC Address (Cisco format) |
|
Find endpoint MACs in logs |
Authentication result |
|
Filter auth outcomes |
Posture status |
|
Track compliance states |
RADIUS attribute |
|
Extract RADIUS AVPs |
# Find all authentication failures
grep -E 'AuthenticationFailed|Authentication failed' ise-logs.txt
# Extract unique MAC addresses that failed auth
grep -oE '([0-9A-Fa-f]\{2}:)\{5}[0-9A-Fa-f]\{2}' auth-failures.log | sort -u
# Find RADIUS Reject reasons
grep -oP '(?<=Reject-Reason=")[^"]+' radius.log
# Extract session IDs from failed authentications
grep -E 'FAIL' ise.log | grep -oE 'Session[0-9a-f]\{8}/[0-9]+:[0-9]+'
# Count failures per endpoint
grep -oE '([0-9A-Fa-f]\{2}:)\{5}[0-9A-Fa-f]\{2}' failures.log | sort | uniq -c | sort -rn
Network Infrastructure
| Pattern | Regex (ERE) | Use Case |
|---|---|---|
IPv4 Address |
|
Extract IPs from configs |
IPv4 with CIDR |
|
Find subnet definitions |
VLAN ID |
|
Extract VLAN references |
Interface name |
|
Find interface references |
Port number |
|
Extract service ports |
ASN |
|
BGP autonomous system numbers |
# Find all subnets in config
grep -oE '([0-9]\{1,3}\.)\{3}[0-9]\{1,3}/[0-9]\{1,2}' router.conf
# Extract interface configurations
grep -E '^interface (Gi|Te|Fa)' switch.conf
# Find ACL permit/deny statements
grep -E '(permit|deny)\s+(ip|tcp|udp)' firewall.conf
# List all ports mentioned
grep -oE ':[0-9]\{1,5}' config.txt | sort -t: -k2 -n | uniq
# Find OSPF area definitions
grep -oE 'area [0-9]+' ospf.conf | sort -u
Security Analysis
| Pattern | Regex (PCRE) | Use Case |
|---|---|---|
SSH key fingerprint |
|
Validate key fingerprints |
Certificate CN |
|
Extract certificate subjects |
JWT token |
|
Find JWTs in logs |
Base64 encoded |
|
Detect encoded data |
Password pattern |
|
Find exposed credentials |
API key format |
|
Detect API keys |
# Find potential secrets in code
grep -rP '(password|secret|api_key|token)\s*[=:]\s*["\047][^"\047]+' src/
# Extract certificate subjects
openssl x509 -in cert.pem -noout -subject | grep -oP 'CN=\K[^,/]+'
# Find hardcoded IPs
grep -rE '\b([0-9]\{1,3}\.)\{3}[0-9]\{1,3}\b' --include="*.py" src/
# Detect base64 strings (potential secrets)
grep -oE '[A-Za-z0-9+/]\{40,}=\{0,2}' config.yaml
# Find sudo commands in logs
grep -E 'sudo.*COMMAND=' /var/log/auth.log
Log Analysis
| Pattern | Regex | Use Case |
|---|---|---|
ISO timestamp |
|
Match standard timestamps |
Syslog timestamp |
|
Match syslog format |
Log level |
|
Filter by severity |
Error with context |
|
Capture error messages |
HTTP status code |
|
Extract HTTP responses |
Response time |
|
Find latency values |
# Count errors by type
grep -oE 'ERROR: [A-Za-z]+' app.log | sort | uniq -c | sort -rn
# Find slow requests (>1000ms)
grep -P '\d\{4,}ms' access.log
# Extract 5xx errors
grep -E '" 5[0-9]\{2} "' access.log
# Find failed SSH attempts
grep -E 'Failed password' /var/log/auth.log | tail -20
# Extract usernames from auth logs
grep -oP '(?<=for )\w+(?= from)' /var/log/auth.log | sort | uniq -c
Personal Life Applications
Note Organization
| Pattern | Regex | Use Case |
|---|---|---|
TODO items |
|
Find action items |
Questions |
|
Find questions asked |
Dates mentioned |
|
Extract date references |
People mentions |
|
Find person references |
Tags |
|
Extract hashtags |
Links |
|
Find URLs |
# Find all TODOs across notes
grep -rE 'TODO:|FIXME:|NOTE:' ~/notes/
# Find unanswered questions
grep -rE '\?$' ~/notes/ --include="*.md" --include="*.adoc"
# Extract all tags used
grep -roE '#[A-Za-z0-9_-]+' ~/notes/ | cut -d: -f2 | sort | uniq -c | sort -rn
# Find mentions of specific person
grep -ri '@gabriel\|@sarah' ~/notes/
# List all external links
grep -ohE 'https?://[^\s)]+' ~/notes/*.md | sort -u
Journal & Reflections
| Pattern | Regex | Use Case |
|---|---|---|
Gratitude entries |
|
Find gratitude mentions |
Goals mentioned |
|
Extract goal statements |
Accomplishments |
|
Find wins |
Challenges |
|
Find areas of difficulty |
Mood indicators |
|
Track emotional states |
# Find gratitude entries this month
grep -ri 'grateful\|thankful' ~/journal/2026-03-*
# Extract goal statements
grep -riE '(i want to|i will|my goal|planning to)' ~/journal/
# Count positive vs negative entries
echo "Positive: $(grep -riEc 'happy|excited|grateful|accomplished' ~/journal/)"
echo "Challenges: $(grep -riEc 'struggle|difficult|frustrated|anxious' ~/journal/)"
# Find entries mentioning specific people
grep -rin 'gabriel\|sarah' ~/journal/2026-*
Financial Tracking
| Pattern | Regex | Use Case |
|---|---|---|
Dollar amounts |
|
Extract monetary values |
Percentage |
|
Find percentages |
Account numbers |
|
Detect card numbers (to redact) |
Transaction date |
|
Extract US date format |
Category prefix |
|
Find categorized expenses |
# Find all dollar amounts
grep -oE '\$[0-9,]+(\.[0-9]\{2})?' expenses.txt
# Sum expenses (requires awk)
grep -oE '\$[0-9,]+(\.[0-9]\{2})?' expenses.txt | \
sed 's/[$,]//g' | awk '{sum+=$1} END {print "$"sum}'
# Find recurring expenses
grep -E '(monthly|recurring|subscription)' budget.md
# Detect and mask card numbers
sed -E 's/[0-9]\{4}[- ]?[0-9]\{4}[- ]?[0-9]\{4}[- ]?[0-9]\{4}/****-****-****-****/g' file.txt
Health & Fitness
| Pattern | Regex | Use Case |
|---|---|---|
Weight entry |
|
Extract weight logs |
Workout duration |
|
Find exercise duration |
Sleep time |
|
Track sleep |
Steps count |
|
Extract step counts |
Medication |
|
Track medication |
# Extract weight entries
grep -oE '[0-9]\{2,3}(\.[0-9])? ?(lbs?|kg)' health-log.txt
# Find workout entries
grep -E '(workout|exercise|gym|run|walk)' ~/journal/2026-*
# Calculate average sleep (last 7 days)
grep -oE '[0-9]+(\.[0-9])? hours? sleep' ~/journal/*.md | \
grep -oE '[0-9]+(\.[0-9])?' | \
awk '{sum+=$1; count++} END {print sum/count " hours avg"}'
Time Management
| Pattern | Regex | Use Case |
|---|---|---|
Time block |
|
Find time ranges |
Duration |
|
Parse durations |
Meeting time |
|
Extract meeting times |
Deadline |
|
Find deadlines |
Pomodoro |
|
Track pomodoro sessions |
# Find time blocks in schedule
grep -oE '[0-9]\{1,2}:[0-9]\{2}\s*-\s*[0-9]\{1,2}:[0-9]\{2}' schedule.md
# Extract meeting mentions
grep -E '(meeting|call|sync|standup)' ~/calendar/*.ics
# Find deadlines
grep -riE '(due|deadline|by) (jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)' ~/notes/
# Calculate daily coding time (from time logs)
grep -E 'code|programming|dev' time-log.txt | \
grep -oE '[0-9]+h' | sed 's/h//' | awk '{sum+=$1} END {print sum "h total"}'
Contact & Communication
| Pattern | Regex | Use Case |
|---|---|---|
Email address |
|
Extract emails |
Phone (US) |
|
Find phone numbers |
Name format |
|
Find full names |
Follow-up mention |
|
Find follow-up tasks |
# Extract all email addresses from notes
grep -rohE '[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]\{2,}' ~/notes/ | sort -u
# Find phone numbers
grep -oE '\(?[0-9]\{3}\)?[-. ]?[0-9]\{3}[-. ]?[0-9]\{4}' contacts.txt
# Find pending follow-ups
grep -ri 'follow.?up\|reach out\|need to contact' ~/notes/ ~/journal/
Tool-Specific Workflows
grep - Search & Extract
# Search recursively with file type filter
grep -rE 'pattern' --include="*.py" --include="*.sh" src/
# Count matches per file
grep -rc 'ERROR' logs/*.log | grep -v ':0$'
# Show context around matches
grep -C 3 'Exception' app.log
# Inverse match (lines NOT matching)
grep -v '^#' config.conf # Remove comments
sed - Transform
# Replace in place
sed -i 's/old/new/g' file.txt
# Delete lines matching pattern
sed '/^#/d' config.conf > clean.conf
# Extract between patterns
sed -n '/START/,/END/p' file.txt
# Multiple substitutions
sed -e 's/foo/bar/g' -e 's/baz/qux/g' file.txt
awk - Analyze
# Extract specific field
awk -F: '{print $1}' /etc/passwd
# Filter and sum
awk '$3 > 100 {sum += $3} END {print sum}' data.txt
# Pattern matching with action
awk '/ERROR/ {count++} END {print count}' log.txt
# Field extraction with regex
awk 'match($0, /[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/) {print substr($0, RSTART, RLENGTH)}' log.txt
vim - Edit
" Search with magic mode (ERE-like)
/\v\d\{1,3}\.\d\{1,3}\.\d\{1,3}\.\d\{1,3}
" Global substitute
:%s/old/new/g
" Delete lines matching pattern
:g/^#/d
" Extract matches to register
:let @a='' | g/pattern/y A
Python re - Script
import re
text = open('log.txt').read()
# Find all
ips = re.findall(r'\d\{1,3}\.\d\{1,3}\.\d\{1,3}\.\d\{1,3}', text)
# Search with groups
match = re.search(r'user=(\w+)', text)
if match:
username = match.group(1)
# Replace
clean = re.sub(r'password=\S+', 'password=***', text)
# Named groups
pattern = r'(?P<date>\d\{4}-\d\{2}-\d\{2}) (?P<level>\w+) (?P<msg>.*)'
for match in re.finditer(pattern, text):
print(match.groupdict())
Pattern Library
Quick Copy Patterns
| What | Pattern |
|---|---|
IPv4 |
|
MAC (colon) |
|
|
|
URL |
|
ISO Date |
|
ISO Timestamp |
|
Dollar Amount |
|
Phone (US) |
|
UUID |
|
Username |
|
Hashtag |
|
Practice Exercises
Professional Exercise Set
-
ISE Log Mining: Extract all unique MAC addresses that failed authentication
-
Network Audit: Find all interfaces configured with trunk mode
-
Security Scan: Detect potential hardcoded passwords in config files
-
Performance: Find all requests taking >500ms from access logs
Personal Exercise Set
-
Note Audit: Find all TODO items across your notes directory
-
Financial: Extract and sum all dollar amounts from expense reports
-
Journal: Find entries mentioning specific goals or achievements
-
Contacts: Build a list of all email addresses mentioned in your notes