Practical RegEx Cheat Sheet

Cyberspecs
3 min readSep 15, 2023

--

I like keeping notes. Making a publicly accessible repository of handy notes. Help yourselves!

Extracting IP Addresses: Use \b(?:\d{1,3}\.){3}\d{1,3}\b to efficiently extract IPv4 addresses from log files.

Parsing URLs: Employ https?:\/\/[^\s/$.?#].[^\s]* to accurately parse URLs from text.

Validating Email Addresses: Utilize [A-Za-z0–9._%+-]+@[A-Za-z0–9.-]+\.[A-Z|a-z]{2,} for reliable email address validation.

Searching for Keywords: Identify target keywords using \b(keyword1|keyword2)\b.

Extracting Dates: Implement (\d{1,2})/(\d{1,2})/(\d{4}) to effortlessly extract dates in “MM/DD/YYYY” format.

Parsing File Paths: Utilize [A-Z]:\\(?:[^\\/:*?”<>|\r\n]+\\)*[^\\/:*?”<>|\r\n]* to seamlessly parse Windows file paths.

Matching MAC Addresses: Use ([0–9A-Fa-f]{2}[:-]){5}([0–9A-Fa-f]{2}) to accurately match MAC addresses.

Validating Phone Numbers: Employ \d{3}-\d{3}-\d{4} for precise validation of US phone numbers.

Extracting HTML Tags: Utilize <([A-Z][A-Z0–9]*)\b[^>]*>(.*?)<\/\1> to extract content from HTML tags.

Identifying Protocol Ports: Use \b\d{1,5}\b to reliably identify protocol port numbers.

Extracting Usernames: Implement @\w+ to extract usernames from social media content.

Capturing Function Names: Use function\s+(\w+)\s*\( to effectively capture function names in code.

Validating Credit Card Numbers: Employ \b(?:\d[ -]*?){13,16}\b to rigorously validate credit card numbers.

Parsing XML Elements: Utilize <(\w+)>.*?<\/\1> to efficiently extract content from XML elements.

Extracting Hashtags: Implement (?<=#)\w+ to accurately extract hashtags from social media posts.

Matching URL Domains:

Use (https?:\/\/)?(www\.)?([a-zA-Z0–9.-]+)\.[a-z]{2,6} to match and extract domain names from URLs.

Finding Function Calls: Employ (\w+)\s*\( to find and extract function calls in code.

Validating IPv6 Addresses: Use [A-Fa-f0–9]{1,4}(:[A-Fa-f0–9]{1,4}){7} to validate IPv6 addresses.

Parsing JSON Keys: Implement \”(\w+)\”: to extract JSON keys from data.

Extracting CSS Class Names: Use \.([A-Za-z][A-Za-z0–9-_]*) to extract class names from CSS.

Matching Dates with Time: Utilize \d{2}\/\d{2}\/\d{4}\s\d{2}:\d{2}:\d{2} to match date-time combinations.

Extracting URLs from HTML: Implement <a\s+(?:[^>]*?\s+)?href=”([^”]*)”, capturing URLs from HTML links.

Identifying Hex Color Codes: Use #([A-Fa-f0–9]{6}|[A-Fa-f0–9]{3}) to identify and capture hex color codes.

Extracting Function Parameters: Utilize (\w+)\s*=\s*[“‘]([^”’]*)[“‘] to capture function parameters.

Matching SQL Keywords: Employ \b(SELECT|FROM|WHERE|JOIN)\b to match SQL query keywords.

Extracting Mentioned Users: Use @([A-Za-z0–9_]+) to extract mentioned users from text.

Parsing JSON Arrays: Utilize \”(\w+)\”:\s*\[([^\]]+)\] to parse JSON arrays.

Extracting Data from CSV: Implement “(.*?)” to capture data within double quotes in CSV.

Matching HTML Comments: Use <! — (.*?) → to match and capture HTML comments.

Parsing Python Docstrings: Utilize [‘“](.*?)[‘“] to parse text within single

--

--