Skillversitas

Regular Expressions

A regular expression (regex) is a sequence of characters that forms a search pattern. Python provides the re module for regex operations.

Regex is very useful for processing large amounts of text, such as validating the format of user input, extracting data from server logs, or cleaning raw data. With a single regex pattern, you can perform searches that would take dozens of lines of ordinary code. Although it looks complicated at first, mastering regex significantly improves productivity in text processing.

The re module provides several main functions: re.search() finds the first pattern match in the whole string, re.match() matches the pattern only from the beginning of the string, re.findall() returns all matches as a list, re.sub() replaces matches with a new string, and re.split() splits a string based on a pattern. Regex patterns are written as raw strings (r"...") so backslashes are not interpreted as Python escape characters.

Code Example

import re

# Matching a pattern
pattern = r"\d{3}-\d{4}"  # phone number format
text = "Call 021-1234 or 031-5678"
matches = re.findall(pattern, text)
print(matches)  # ['021-1234', '031-5678']

# Searching & replacing
text2 = "Email: [email protected] and [email protected]"
emails = re.findall(r"[\w.]+@[\w.]+", text2)
print(emails)  # ['[email protected]', '[email protected]']

# Replacing
new_text = re.sub(r"\d+", "***", "There are 5 cats and 3 dogs")
print(new_text)  # There are *** cats and *** dogs

# Validation
if re.match(r"^\d{10}$", "0812345678"):
    print("Valid number")

Code Explanation

First, the re module is imported. The pattern r"\d{3}-\d{4}" looks for three digits, a hyphen, then four digits — suitable for a simple phone number format. re.findall() returns all matches in a list. The pattern r"[\w.]+@[\w.]+" looks for one or more word characters or dots, followed by @, then one or more word characters or dots — suitable for a simple email address. re.sub() replaces all digit sequences (\d+) with "***". re.match() with the pattern r"^\d{10}$" validates that the string consists of exactly 10 digits from the start (^) to the end ($).

Everyday Example

A user registration application needs to validate form input. Phone numbers are checked with the pattern r"^08\d{8,11}$" to ensure a valid Indonesian format. Email addresses are validated with a pattern that ensures there are characters before and after @ as well as a valid domain. Passwords are checked for strength with a combined pattern: at least one uppercase letter, one digit, and a minimum length of 8 characters. All of these validations are done with regex before data is saved to the database, preventing junk data from entering the system.

Summary

Regular expressions are a very powerful tool for searching and manipulating text. Python's re module provides complete functions for pattern-based matching, searching, replacing, and splitting, which are essential for input validation and text data processing.