> For the complete documentation index, see [llms.txt](https://riteshs4hu.gitbook.io/infosec-notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://riteshs4hu.gitbook.io/infosec-notes/language/python/library/re-regex.md).

# re (Regex)

Regular Expressions (**regex**) are powerful tools for **pattern matching and text manipulation**. They are often used to **search**, **extract**, **replace**, or **filter specific data** from files and text outputs.

***

### **1. What is Regex?**

Regex (Regular Expression) is a **sequence of characters** that defines a **search pattern**. Python provides the **`re` module** to work with regular expressions.

***

### **2. Importing the `re` Module**

```python
import re
```

***

### **3. Common Regex Functions**

| Function        | Description                                                               | Example                             |
| --------------- | ------------------------------------------------------------------------- | ----------------------------------- |
| `re.match()`    | Matches pattern only at the **beginning** of a string                     | `re.match(r"abc", "abcdef")`        |
| `re.search()`   | Searches for the **first occurrence** of a pattern anywhere in the string | `re.search(r"abc", "123abc456")`    |
| `re.findall()`  | Returns **all occurrences** of the pattern as a list                      | `re.findall(r"\d+", "A1 B22 C333")` |
| `re.finditer()` | Returns an **iterator** yielding match objects                            | `re.finditer(r"[A-Z]", "Hello")`    |
| `re.sub()`      | Replaces pattern occurrences with another string                          | `re.sub(r"\d", "#", "A1B2C3")`      |
| `re.split()`    | Splits string by pattern                                                  | `re.split(r"\s+", "Python is fun")` |

***

### **4. Basic Regex Patterns**

| Pattern | Meaning                             | Example Match                                         |
| ------- | ----------------------------------- | ----------------------------------------------------- |
| `.`     | Any character except newline        | `"a.c"` → `abc`, `a_c`                                |
| `^`     | Start of string                     | `"^Hello"` matches only if string starts with “Hello” |
| `$`     | End of string                       | `"end$"` matches only if string ends with “end”       |
| `*`     | 0 or more occurrences               | `"ab*c"` → `ac`, `abc`, `abbc`                        |
| `+`     | 1 or more occurrences               | `"ab+c"` → `abc`, `abbc`                              |
| `?`     | 0 or 1 occurrence                   | `"colou?r"` → `color`, `colour`                       |
| `{n}`   | Exactly n occurrences               | `\d{3}` → matches 3 digits                            |
| `{n,}`  | n or more occurrences               | `\d{2,}`                                              |
| `{n,m}` | Between n and m occurrences         | `\d{2,4}`                                             |
| `[]`    | Character set                       | `[aeiou]` matches any vowel                           |
| \`      | \`                                  | OR operator                                           |
| `()`    | Grouping                            | `(abc)+` matches one or more “abc”                    |
| `\d`    | Digit \[0–9]                        | `123`                                                 |
| `\D`    | Non-digit                           | `abc`                                                 |
| `\w`    | Word character \[A–Z, a–z, 0–9, \_] | `python3`                                             |
| `\W`    | Non-word character                  | `@!#`                                                 |
| `\s`    | Whitespace                          | space, tab, newline                                   |
| `\S`    | Non-whitespace                      | text without spaces                                   |

***

### **5. Example: Simple Pattern Search**

```python
import re

text = "Python 3.10 is released in 2021."
pattern = r"\d+"  # Find all numbers

matches = re.findall(pattern, text)
print(matches)
```

**Output:**

```
['3', '10', '2021']
```

***

### **6. Example: Extract Email Addresses**

```python
import re

data = """
Contact us at support@example.com or admin@test.org
"""

emails = re.findall(r"[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}", data)
print(emails)
```

**Output:**

```
['support@example.com', 'admin@test.org']
```

***

### **7. Example: Validate IP Address**

```python
import re

ip = "192.168.0.1"

pattern = r"^(\d{1,3}\.){3}\d{1,3}$"

if re.match(pattern, ip):
    print("Valid IP address")
else:
    print("Invalid IP address")
```

**Output:**

```
Valid IP address
```

***

### **8. Extracting Data from a File Using Regex**

You can combine **file I/O** with regex to **filter specific lines** from large logs or outputs.

#### **Example: Extract All Email Addresses from a File**

```python
import re

with open("users.txt", "r") as file:
    data = file.read()

emails = re.findall(r"[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}", data)

with open("emails.txt", "w") as output:
    for email in emails:
        output.write(email + "\n")
```

✅ Reads from a file → filters with regex → writes filtered results to a new file.

***

### **9. Filtering Lines Matching a Pattern**

#### **Example: Filter Log Lines Containing “ERROR”**

```python
import re

with open("app.log", "r") as file:
    lines = file.readlines()

with open("error_log.txt", "w") as output:
    for line in lines:
        if re.search(r"ERROR", line):
            output.write(line)
```

**Output file (`error_log.txt`):**

```
[2025-10-07] ERROR: Connection failed.
[2025-10-07] ERROR: Timeout reached.
```

***

### **10. Filtering IPs from a Log File**

```python
import re

with open("network.log", "r") as file:
    data = file.read()

ips = re.findall(r"\b\d{1,3}(?:\.\d{1,3}){3}\b", data)

with open("ips.txt", "w") as output:
    output.write("\n".join(ips))
```

**Example Output (`ips.txt`):**

```
192.168.0.1
10.0.0.5
172.16.1.2
```

***

### **11. Using Flags in Regex**

| Flag                      | Description                      |
| ------------------------- | -------------------------------- |
| `re.IGNORECASE` or `re.I` | Case-insensitive matching        |
| `re.MULTILINE` or `re.M`  | `^` and `$` match at line breaks |
| `re.DOTALL` or `re.S`     | `.` matches newlines too         |

#### **Example**

```python
re.findall(r"python", "Python is fun", re.I)
```

**Output:**

```
['Python']
```

***

### **12. Example: Replace Sensitive Data**

```python
import re

data = "User: Alice, Password: secret123"

masked = re.sub(r"Password:\s+\w+", "Password: ******", data)
print(masked)
```

**Output:**

```
User: Alice, Password: ******
```

***

### **13. Combining Regex with OS & File Modules**

You can combine regex with modules like `os` for automation or report generation.

#### **Example: Search All `.log` Files for Errors**

```python
import os, re

for file in os.listdir("."):
    if file.endswith(".log"):
        with open(file, "r") as f:
            for line in f:
                if re.search(r"ERROR|FAIL|CRITICAL", line, re.I):
                    print(f"{file}: {line.strip()}")
```

***

### **14. Summary**

| Concept              | Function                   | Description                      |
| -------------------- | -------------------------- | -------------------------------- |
| **Pattern Matching** | `re.match()`               | Match at the start               |
| **Searching**        | `re.search()`              | Search anywhere in text          |
| **Find All Matches** | `re.findall()`             | Returns list of all matches      |
| **Replace**          | `re.sub()`                 | Substitute pattern with string   |
| **Split**            | `re.split()`               | Split text by pattern            |
| **File Filtering**   | Combine `re` with file I/O | Extract lines, emails, IPs, etc. |
