Loading notes...
Loading notes...
B.Tech • Chapter 10
B.Tech level module on Regular Expressions & Parsing.
Regular Expressions (Regex) are powerful strings of characters that define search patterns. They are essential for data validation, parsing log files, and text processing in backend systems.
Advanced Engineering Concepts
The 're' module provides full support for Perl-like regular expressions. Compiling a regex pattern using re.compile() improves performance if the pattern is used repeatedly in a loop. Regex utilizes greedy (matching as much text as possible) and non-greedy (matching as little as possible) quantifiers, lookaheads, and lookbehinds for complex string assertions without consuming characters.
Code Implementation
import re
log_data = "ERROR 404 at 10:45:01, IP: 192.168.1.1. INFO 200 at 10:46:00, IP: 10.0.0.5"
# Extract all IP addresses using a raw string pattern
# Note: r"..." is crucial to prevent Python from interpreting escape sequences
ip_pattern = re.compile(r"\b(?:\d{1,3}\.){3}\d{1,3}\b")
ips = ip_pattern.findall(log_data)
print("Found IPs:", ips)
# Named capturing groups
auth_pattern = re.compile(r"(?P<status>ERROR|INFO)\s(?P<code>\d{3})")
for match in auth_pattern.finditer(log_data):
print(match.groupdict())