PYTHON Reference Guide
Revision Time: 3 mins

Regex Reference

Regular expression pattern mappings on text streams.

re.search vs re.match
Return: Match / None

Compare pattern matching at the start of a string vs anywhere in the string.

Used In: Header matching, pattern verification.

Syntax signature:re.search(pat, s) / re.match(pat, s)
Code snippet:
python
import re
s = 'ID: 100'
print(bool(re.match('100', s)), bool(re.search('100', s)))
Expected Output:False True

Time Complexity: O(N)

Related Methods: re.search()

Remember: match() only checks for a match at the beginning of the string; search() checks the entire string.
re.findall
Return: List[str]

Find all substring matches for a regex pattern.

Used In: Parsing logs, extracting numbers or email IDs.

Syntax signature:re.findall(pattern, string)
Code snippet:
python
import re
print(re.findall(r'\d+', 'User 123 has id 456'))
Expected Output:['123', '456']

Time Complexity: O(N) where N is characters count

Common Mistakes: Not escaping backslashes inside regex patterns (use raw string r'...' to prevent issues).

Related Methods: re.finditer()

Remember: Compiling patterns using re.compile() is faster for repeated reuse.
re.finditer
Return: iterator

Find all matching occurrences as an iterator of match objects.

Used In: Streaming log analysis.

Syntax signature:re.finditer(pattern, string)
Code snippet:
python
import re
matches = re.finditer(r'\d+', 'id 10, id 20')
print([m.group() for m in matches])
Expected Output:['10', '20']

Time Complexity: O(N)

Related Methods: re.findall()

Remember: Yields match objects lazily, saving memory compared to findall() on large files.
re.sub
Return: str

Substitute occurrences of a pattern with a replacement string.

Used In: Normalizing dirty text fields, removing illegal filename characters.

Syntax signature:re.sub(pattern, repl, string)
Code snippet:
python
import re
print(re.sub(r'\s+', '_', 'a  b   c'))
Expected Output:'a_b_c'

Time Complexity: O(N)

Related Methods: str.replace()

Remember: Useful for replacing multiple whitespace characters with single formatting separators.
re.compile
Return: Pattern

Compile a regular expression pattern into a reusable RegexObject.

Used In: High-throughput log analysis loops.

Syntax signature:re.compile(pattern)
Code snippet:
python
import re
pat = re.compile(r'\d+')
print(pat.findall('10 records, 20 columns'))
Expected Output:['10', '20']

Time Complexity: O(1) after compilation

Remember: Saves parsing overhead when matching the same expression thousands of times in a loop.
Capturing Groups
Return: Tuple[str]

Extract specific parts of a match using parentheses ().

Used In: Parsing structured key-value codes from strings.

Syntax signature:match.groups() / match.group(i)
Code snippet:
python
import re
m = re.search(r'(\d+)-(\w+)', '123-OOM')
print(m.groups())
Expected Output:('123', 'OOM')

Time Complexity: O(N)

Related Methods: Named Groups

Remember: group(0) is the entire match; group(1) is the first captured parenthesized sub-expression.
Named Groups
Return: Dict[str, str]

Access captured sub-expression matches using readable dictionary keys.

Used In: Parsing structured log lines into dictionary inputs.

Syntax signature:match.groupdict() / (?P<name>pattern)
Code snippet:
python
import re
m = re.search(r'(?P<id>\d+)-(?P<tag>\w+)', '123-OOM')
print(m.groupdict())
Expected Output:{'id': '123', 'tag': 'OOM'}

Time Complexity: O(N)

Related Methods: Capturing Groups

Remember: Increases regex readability by indexing matches by logical name rather than indices.
Common Regex Patterns
Return: List[str]

List standard regex patterns for dates, emails, IPs, and phone numbers.

Used In: Data validation, scraping log files.

Syntax signature:re.findall(pattern, text)
Code snippet:
python
import re
ip_pat = r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}'
print(re.findall(ip_pat, 'IP: 192.168.1.1'))
Expected Output:['192.168.1.1']

Time Complexity: O(N)

Remember: Always test complex regex pattern boundary cases in validation files first.