Ever wished you could instantly extract all email addresses from a document or clean up messy data with a single command? Regex makes that possible. Regular expressions (often shortened to “regex”) are powerful tools for searching, matching, and manipulating patterns within text strings. While the name might sound intimidating, think of regex as a search language that uses special characters and symbols to describe complex patterns. They’re used in programming languages, text editors, and command-line tools to find and modify text quickly. Learning to read and write regex patterns can dramatically improve your efficiency when working with data, files, or code. This article will break down the basics of regex, show you how to get started, explain flags that modify how patterns match (including multiline and global), and provide guidance on how to keep getting better. You’ll also explore practical examples, like validating social media handles and a simple email pattern. What Are Regular Expressions? Regular expressions are sequences of characters that define a search pattern. Think of regex patterns as templates that describe how the text you’re looking for should look. You might have already used simpler patterns like *.txt to find all files ending in .txt. Regex takes that idea much further, allowing you to define the rules in detail. For example: Find all email addresses in a block of text Extract Twitter or Instagram handles Validate input in a form, such as phone numbers or dates At its core, a regex helps you answer: “Does this text match a certain pattern?” or “Where in this text can I find a certain pattern?” Getting Started with Regex Start Small and Understand the Basics Before diving into complex patterns, get comfortable with a few key concepts: Literal Characters: Literal characters match themselves. For example, the regex cat will match the letters ‘c’ followed by ‘a’ followed by ‘t’ in the text. Example: Regex: cat Matches: "cat" in "My cat is cute." Does not match: "Cat" (capital ‘C’) without a case-insensitive flag. To make it match regardless of case, you could use (?i)cat (in some flavors) or a flag like /i in JavaScript (/cat/i). Special Characters (Metacharacters): Characters like . (dot), * (asterisk), ? (question mark), + (plus), and | (pipe) have special meanings. For example, . matches any single character except a newline. Important: If you need to match a character that has special meaning, you must escape it with a backslash \. For example, . matches any character, but \. matches a literal period.Example: Regex: c.t Matches: "cat", "cot", "cut" in "The cat sat on a cot next to a cut log." If you wanted to match "c.t" literally (with a dot), you’d write c\.t. Character Sets and Ranges: Square brackets [ ] let you match any one character from a set. For instance, [abc] matches 'a', 'b', or 'c'; [0-9] matches any digit from 0 to 9. Anchors: ^ matches the start of a line, and $ matches the end of a line. They ensure your match occurs at a specific position. Grouping: Parentheses ( ) let you group parts of your regex. This can be useful for applying quantifiers to entire groups or for capturing matched content. Capturing group example: (ab)+ matches "ab", "abab", "ababab". Non-capturing groups: (?:ab)+ Works similarly but doesn’t capture the matched text. This is useful when you need grouping but do not need to extract the matches from that group. Non-capturing groups are an advanced feature that can make complex patterns more efficient and easier to manage. Use Online Tools for Instant Feedback Online regex testers help you learn quickly by letting you type in a pattern, provide sample text, and instantly see what matches. Some popular tools are: Regex101 Regexr Rubular Experimenting with these tools is one of the fastest ways to improve your understanding. You can modify your pattern, see what changes, and get immediate feedback. Check the Documentation for Your Environment Different programming languages and tools may have slightly different regex flavors. Always check the documentation for the environment you’re using: JavaScript: MDN Web Docs on Regular Expressions Python: Python’s re Module Documentation Java: Java Tutorials on Regex Knowing which features are available and how they differ across environments will ensure your patterns work as intended wherever you apply them. Key Concepts and Simple Examples Literal Matches: Regex: cat Matches: “cat” in “My cat is cute.” Using Metacharacters: Regex: c.t The . matches any single character. Matches: “cat”, “cot”, “cut” in “The cat sat on a cot next to a cut log.” Character Sets and Ranges: Regex: [aeiou] matches any single vowel. Regex: [A-Za-z0-9_] matches letters, digits, and underscore. Repetition Quantifiers: Regex: a+ The + means one or more occurrences of a. Matches: “a”, “aa”, “aaa”. Regex: ca*t The * means zero or more occurrences of a. Matches: “ct” (no a), “cat” (one a), “caat” (two as), etc. Anchors: Regex: ^Hello Matches “Hello” only if it’s at the start of the line. Regex: end$ Matches “end” only if it’s at the end of the line. Grouping and Capturing: Grouping and capturing allows you to collect specific parts of the text that match a pattern. These are especially useful when you need to extract data from a string. Regex: (\d{3}) e.g. Staff code Explanation: The parentheses () create a group, and \d{3} matches exactly three digits. Example Match: "123" This will match because "123" is exactly three digits, and the group captures the digits. Captured Group: "123" The matched digits are captured as a group. Regex: ([A-Za-z]{2})(\d{4}) Explanation: This pattern has two groups: eg a product SKU ([A-Za-z]{2}) captures two letters (e.g., the category). (\d{4}) captures four digits (e.g., the product number). Example Match: "AB1234" This will match because it starts with two letters, followed by four digits. Captured Group 1: "AB" — The category letters. Captured Group 2: "1234" — The product number. Greedy vs. Lazy Quantifiers: Greedy Quantifier: .* tries to match as much text as possible. Lazy Quantifier: .*? tries to match as little text as possible. Example: Given the string "cat in a hat": c.*t will match "cat in a hat" in its entirety (greedy), because .* stretches to include everything up to the last 't'. c.*?t will match only "cat" (lazy), because .*? stops as soon as it finds a 't', resulting in the smallest possible match. Important Regex Flags: Multiline and Global Flags modify how the regex engine interprets the pattern or processes the text. Flag Meaning Example Notes g Global (find all matches) /cat/g Without g, the engine stops after the first match. m Multiline /^R.*/m ^ and $ match the start/end of every line, not just the string. i Case-insensitive /cat/i Matches "Cat", "CAT", etc. s Dotall (single-line) /cat/s . matches newline characters as well. x Extended (verbose) mode Depends on flavor Allows whitespace/comments for readability (not in all flavors). Note: Not all regex flavors support all flags. For example, x is common in some languages (like Perl or PCRE) but not in others. Always check your language-specific documentation. For JavaScript, the commonly available flags are g, m, i, s, and u. Applying Flags in Examples Multiline Flag (m): If you have: Hello world Regex is fun Using ^Regex without /m won’t match because "Regex" doesn’t appear at the start of the entire string. With /m, ^Regex matches the start of the second line therefore it’ll match. Global Flag (g): Given "banana", /a/ finds only the first 'a', but /a/g finds all three 'a's. Real-World Examples Matching a Twitter Handle Problem: Match a Twitter handle. A typical Twitter handle: Starts with @ Followed by letters, digits, or underscore Up to 15 characters long Regex: ^@[A-Za-z0-9_]{1,15}$ Explanation: ^ and $ ensure you match the entire string. @ matches the literal “@” symbol. [A-Za-z0-9_] allows letters, digits, and underscore. {1,15} limits length to 1–15 characters. Examples: Matches: @john_doe, @User123 Does not match: john_doe (no @), @thisUsernameIsTooLongForTwitterHandles Matching an Instagram Handle Problem: Instagram handles: Starts with @ Start and contain letters, numbers, underscores, and periods Up to 30 characters Regex: ^@[A-Za-z0-9_.]{1,30}$ Explanation: ^ and $ for full string match. @ matches the literal “@” symbol. [A-Za-z0-9_.] allows letters, digits, underscore, and period. {1,30} sets length limit. Examples: Matches: @jane.doe, @user_123, @_insta_guy Does not match: insta (no @), jane%doe (invalid % character) A Simple Email Validator Problem: Basic email pattern: <username>@<domain>.<tld> Regex: ^[A-Za-z0-9._%+\-]+@[A-Za-z0-9.\-]+\.[A-Za-z]{2,}$ Explanation: [A-Za-z0-9._%+\-]+: Username with allowed characters (., _, %, +, -). @: Literal “@”. [A-Za-z0-9.\-]+: Domain name with letters, digits, dots, and hyphens. \.[A-Za-z]{2,}: A literal dot followed by at least two letters. Examples: Matches: jane.doe@example.com, user_123@my-site.org Does not match: jane.doe@, @domain.com, user@domain Real-world email validation is more complex, but this is a good starting point. Tips for Improving Your Regex Skills Start Simple, Then Build Up: Begin with small patterns and add complexity as you understand each part. Use Comments and Verbose Mode (If Available): Some regex flavors allow a verbose mode (e.g., the x flag in some languages) where you can space out your pattern and add comments. This is invaluable for maintaining complex regexes. Test on Multiple Examples: Use positive (should match), negative (should not match), and edge-case examples to ensure your regex works as intended. Learn Common Patterns: Familiarize yourself with frequently needed patterns (such as email addresses, phone numbers, and URLs). Having a library of known patterns saves time. Study Documentation and Reference Guides: Keep a reference handy. Tools like Regular-Expressions.info offer comprehensive tutorials and references. Practice, Practice, Practice: The more you use regex, the more intuitive it becomes. Challenge yourself by solving common text parsing or validation problems. Conclusion Regular expressions might seem daunting, but starting with the basics and gradually exploring more complex patterns and flags will help you master this powerful tool. Use online tools, consult documentation for your environment, and practice regularly. Soon, you’ll go from avoiding regex to confidently using it as one of your essential tools. Happy matching! Ever wished you could instantly extract all email addresses from a document or clean up messy data with a single command? Regex makes that possible. Regular expressions (often shortened to “regex”) are powerful tools for searching, matching, and manipulating patterns within text strings. While the name might sound intimidating, think of regex as a search language that uses special characters and symbols to describe complex patterns. They’re used in programming languages, text editors, and command-line tools to find and modify text quickly. Learning to read and write regex patterns can dramatically improve your efficiency when working with data, files, or code. This article will break down the basics of regex , show you how to get started, explain flags that modify how patterns match (including multiline and global), and provide guidance on how to keep getting better. You’ll also explore practical examples, like validating social media handles and a simple email pattern. regex What Are Regular Expressions? Regular expressions are sequences of characters that define a search pattern. Think of regex patterns as templates that describe how the text you’re looking for should look. You might have already used simpler patterns like *.txt to find all files ending in .txt . Regex takes that idea much further, allowing you to define the rules in detail. *.txt .txt For example: For example: Find all email addresses in a block of text Extract Twitter or Instagram handles Validate input in a form, such as phone numbers or dates Find all email addresses in a block of text Extract Twitter or Instagram handles Validate input in a form, such as phone numbers or dates At its core, a regex helps you answer: “Does this text match a certain pattern?” or “Where in this text can I find a certain pattern?” “Does this text match a certain pattern?” “Where in this text can I find a certain pattern?” Getting Started with Regex Start Small and Understand the Basics Before diving into complex patterns, get comfortable with a few key concepts: Literal Characters: Literal characters match themselves. For example, the regex cat will match the letters ‘c’ followed by ‘a’ followed by ‘t’ in the text. Example: Regex: cat Matches: "cat" in "My cat is cute." Does not match: "Cat" (capital ‘C’) without a case-insensitive flag. To make it match regardless of case, you could use (?i)cat (in some flavors) or a flag like /i in JavaScript (/cat/i). Special Characters (Metacharacters): Characters like . (dot), * (asterisk), ? (question mark), + (plus), and | (pipe) have special meanings. For example, . matches any single character except a newline. Important: If you need to match a character that has special meaning, you must escape it with a backslash \. For example, . matches any character, but \. matches a literal period.Example: Regex: c.t Matches: "cat", "cot", "cut" in "The cat sat on a cot next to a cut log." If you wanted to match "c.t" literally (with a dot), you’d write c\.t. Character Sets and Ranges: Square brackets [ ] let you match any one character from a set. For instance, [abc] matches 'a', 'b', or 'c'; [0-9] matches any digit from 0 to 9. Anchors: ^ matches the start of a line, and $ matches the end of a line. They ensure your match occurs at a specific position. Grouping: Parentheses ( ) let you group parts of your regex. This can be useful for applying quantifiers to entire groups or for capturing matched content. Capturing group example: (ab)+ matches "ab", "abab", "ababab". Non-capturing groups: (?:ab)+ Works similarly but doesn’t capture the matched text. This is useful when you need grouping but do not need to extract the matches from that group. Non-capturing groups are an advanced feature that can make complex patterns more efficient and easier to manage. Literal Characters: Literal characters match themselves. For example, the regex cat will match the letters ‘c’ followed by ‘a’ followed by ‘t’ in the text. Example: Regex: cat Matches: "cat" in "My cat is cute." Does not match: "Cat" (capital ‘C’) without a case-insensitive flag. To make it match regardless of case, you could use (?i)cat (in some flavors) or a flag like /i in JavaScript (/cat/i). Literal Characters: Literal characters match themselves. For example, the regex cat will match the letters ‘c’ followed by ‘a’ followed by ‘t’ in the text. Literal Characters: cat Example: Example: Regex: cat Matches: "cat" in "My cat is cute." Does not match: "Cat" (capital ‘C’) without a case-insensitive flag. To make it match regardless of case, you could use (?i)cat (in some flavors) or a flag like /i in JavaScript (/cat/i). Regex: cat Regex: cat cat Matches: "cat" in "My cat is cute." Matches: "cat" in "My cat is cute." Does not match: "Cat" (capital ‘C’) without a case-insensitive flag. To make it match regardless of case, you could use (?i)cat (in some flavors) or a flag like /i in JavaScript (/cat/i). Does not match: "Cat" (capital ‘C’) without a case-insensitive flag. To make it match regardless of case, you could use (?i)cat (in some flavors) or a flag like /i in JavaScript ( /cat/i ). (?i)cat /i /cat/i Special Characters (Metacharacters): Characters like . (dot), * (asterisk), ? (question mark), + (plus), and | (pipe) have special meanings. For example, . matches any single character except a newline. Important: If you need to match a character that has special meaning, you must escape it with a backslash \. For example, . matches any character, but \. matches a literal period. Example: Regex: c.t Matches: "cat", "cot", "cut" in "The cat sat on a cot next to a cut log." If you wanted to match "c.t" literally (with a dot), you’d write c\.t. Special Characters (Metacharacters): Characters like . (dot), * (asterisk), ? (question mark), + (plus), and | (pipe) have special meanings. For example, . matches any single character except a newline. Special Characters (Metacharacters): . * ? + | . Important: If you need to match a character that has special meaning, you must escape it with a backslash \ . For example, . matches any character, but \. matches a literal period. Important: \ . \. Example: Example: Regex: c.t Matches: "cat", "cot", "cut" in "The cat sat on a cot next to a cut log." If you wanted to match "c.t" literally (with a dot), you’d write c\.t. Regex: c.t c.t Matches: "cat", "cot", "cut" in "The cat sat on a cot next to a cut log." If you wanted to match "c.t" literally (with a dot), you’d write c\.t . c\.t Character Sets and Ranges: Square brackets [ ] let you match any one character from a set. For instance, [abc] matches 'a', 'b', or 'c'; [0-9] matches any digit from 0 to 9. Character Sets and Ranges: Square brackets [ ] let you match any one character from a set. For instance, [abc] matches 'a', 'b', or 'c'; [0-9] matches any digit from 0 to 9. Character Sets and Ranges: [ ] [abc] [0-9] Anchors: ^ matches the start of a line, and $ matches the end of a line. They ensure your match occurs at a specific position. Anchors: ^ matches the start of a line, and $ matches the end of a line. They ensure your match occurs at a specific position. Anchors: ^ $ Grouping: Parentheses ( ) let you group parts of your regex. This can be useful for applying quantifiers to entire groups or for capturing matched content. Capturing group example: (ab)+ matches "ab", "abab", "ababab". Non-capturing groups: (?:ab)+ Works similarly but doesn’t capture the matched text. This is useful when you need grouping but do not need to extract the matches from that group. Non-capturing groups are an advanced feature that can make complex patterns more efficient and easier to manage. Grouping: Parentheses ( ) let you group parts of your regex. This can be useful for applying quantifiers to entire groups or for capturing matched content. Grouping: ( ) Capturing group example: (ab)+ matches "ab", "abab", "ababab". Non-capturing groups: (?:ab)+ Works similarly but doesn’t capture the matched text. This is useful when you need grouping but do not need to extract the matches from that group. Non-capturing groups are an advanced feature that can make complex patterns more efficient and easier to manage. Capturing group example: (ab)+ matches "ab", "abab", "ababab". Capturing group example: (ab)+ Non-capturing groups: (?:ab)+ Works similarly but doesn’t capture the matched text. This is useful when you need grouping but do not need to extract the matches from that group. Non-capturing groups are an advanced feature that can make complex patterns more efficient and easier to manage. Non-capturing groups: (?:ab)+ Use Online Tools for Instant Feedback Online regex testers help you learn quickly by letting you type in a pattern, provide sample text, and instantly see what matches. Some popular tools are: Regex101 Regexr Rubular Regex101 Regexr Rubular Experimenting with these tools is one of the fastest ways to improve your understanding. You can modify your pattern, see what changes, and get immediate feedback. Check the Documentation for Your Environment Different programming languages and tools may have slightly different regex flavors. Always check the documentation for the environment you’re using: JavaScript: MDN Web Docs on Regular Expressions Python: Python’s re Module Documentation Java: Java Tutorials on Regex JavaScript: MDN Web Docs on Regular Expressions MDN Web Docs on Regular Expressions Python: Python’s re Module Documentation Python’s re Module Documentation Java: Java Tutorials on Regex Java Tutorials on Regex Knowing which features are available and how they differ across environments will ensure your patterns work as intended wherever you apply them. Key Concepts and Simple Examples Literal Matches: Regex: cat Matches: “cat” in “My cat is cute.” Using Metacharacters: Regex: c.t The . matches any single character. Matches: “cat”, “cot”, “cut” in “The cat sat on a cot next to a cut log.” Character Sets and Ranges: Regex: [aeiou] matches any single vowel. Regex: [A-Za-z0-9_] matches letters, digits, and underscore. Repetition Quantifiers: Regex: a+ The + means one or more occurrences of a. Matches: “a”, “aa”, “aaa”. Regex: ca*t The * means zero or more occurrences of a. Matches: “ct” (no a), “cat” (one a), “caat” (two as), etc. Anchors: Regex: ^Hello Matches “Hello” only if it’s at the start of the line. Regex: end$ Matches “end” only if it’s at the end of the line. Grouping and Capturing: Grouping and capturing allows you to collect specific parts of the text that match a pattern. These are especially useful when you need to extract data from a string. Regex: (\d{3}) e.g. Staff code Explanation: The parentheses () create a group, and \d{3} matches exactly three digits. Example Match: "123" This will match because "123" is exactly three digits, and the group captures the digits. Captured Group: "123" The matched digits are captured as a group. Regex: ([A-Za-z]{2})(\d{4}) Explanation: This pattern has two groups: eg a product SKU ([A-Za-z]{2}) captures two letters (e.g., the category). (\d{4}) captures four digits (e.g., the product number). Example Match: "AB1234" This will match because it starts with two letters, followed by four digits. Captured Group 1: "AB" — The category letters. Captured Group 2: "1234" — The product number. Greedy vs. Lazy Quantifiers: Greedy Quantifier: .* tries to match as much text as possible. Lazy Quantifier: .*? tries to match as little text as possible. Example: Given the string "cat in a hat": c.*t will match "cat in a hat" in its entirety (greedy), because .* stretches to include everything up to the last 't'. c.*?t will match only "cat" (lazy), because .*? stops as soon as it finds a 't', resulting in the smallest possible match. Literal Matches: Regex: cat Matches: “cat” in “My cat is cute.” Literal Matches: Literal Matches: Regex: cat Matches: “cat” in “My cat is cute.” Regex: cat Regex: cat Matches: “cat” in “My cat is cute.” Matches: Using Metacharacters: Regex: c.t The . matches any single character. Matches: “cat”, “cot”, “cut” in “The cat sat on a cot next to a cut log.” Using Metacharacters: Using Metacharacters: Regex: c.t The . matches any single character. Matches: “cat”, “cot”, “cut” in “The cat sat on a cot next to a cut log.” Regex: c.t The . matches any single character. Matches: “cat”, “cot”, “cut” in “The cat sat on a cot next to a cut log.” Regex: c.t The . matches any single character. Matches: “cat”, “cot”, “cut” in “The cat sat on a cot next to a cut log.” The . matches any single character. . Matches: “cat”, “cot”, “cut” in “The cat sat on a cot next to a cut log.” Character Sets and Ranges: Regex: [aeiou] matches any single vowel. Regex: [A-Za-z0-9_] matches letters, digits, and underscore. Character Sets and Ranges: Character Sets and Ranges: Regex: [aeiou] matches any single vowel. Regex: [A-Za-z0-9_] matches letters, digits, and underscore. Regex: [aeiou] matches any single vowel. Regex: [aeiou] Regex: [A-Za-z0-9_] matches letters, digits, and underscore. Regex: [A-Za-z0-9_] Repetition Quantifiers: Regex: a+ The + means one or more occurrences of a. Matches: “a”, “aa”, “aaa”. Regex: ca*t The * means zero or more occurrences of a. Matches: “ct” (no a), “cat” (one a), “caat” (two as), etc. Repetition Quantifiers: Repetition Quantifiers: Regex: a+ The + means one or more occurrences of a. Matches: “a”, “aa”, “aaa”. Regex: ca*t The * means zero or more occurrences of a. Matches: “ct” (no a), “cat” (one a), “caat” (two as), etc. Regex: a+ The + means one or more occurrences of a. Matches: “a”, “aa”, “aaa”. Regex: a+ The + means one or more occurrences of a. Matches: “a”, “aa”, “aaa”. The + means one or more occurrences of a . + a Matches: “a”, “aa”, “aaa”. Regex: ca*t The * means zero or more occurrences of a. Matches: “ct” (no a), “cat” (one a), “caat” (two as), etc. Regex: ca*t The * means zero or more occurrences of a. Matches: “ct” (no a), “cat” (one a), “caat” (two as), etc. The * means zero or more occurrences of a . * a Matches: “ct” (no a ), “cat” (one a ), “caat” (two a s), etc. a a a Anchors: Regex: ^Hello Matches “Hello” only if it’s at the start of the line. Regex: end$ Matches “end” only if it’s at the end of the line. Anchors: Anchors: Regex: ^Hello Matches “Hello” only if it’s at the start of the line. Regex: end$ Matches “end” only if it’s at the end of the line. Regex: ^Hello Matches “Hello” only if it’s at the start of the line. ^Hello Matches “Hello” only if it’s at the start of the line. Matches “Hello” only if it’s at the start of the line. Regex: end$ Matches “end” only if it’s at the end of the line. end$ Matches “end” only if it’s at the end of the line. Matches “end” only if it’s at the end of the line. Grouping and Capturing: Grouping and capturing allows you to collect specific parts of the text that match a pattern. These are especially useful when you need to extract data from a string. Regex: (\d{3}) e.g. Staff code Explanation: The parentheses () create a group, and \d{3} matches exactly three digits. Example Match: "123" This will match because "123" is exactly three digits, and the group captures the digits. Captured Group: "123" The matched digits are captured as a group. Regex: ([A-Za-z]{2})(\d{4}) Explanation: This pattern has two groups: eg a product SKU ([A-Za-z]{2}) captures two letters (e.g., the category). (\d{4}) captures four digits (e.g., the product number). Example Match: "AB1234" This will match because it starts with two letters, followed by four digits. Captured Group 1: "AB" — The category letters. Captured Group 2: "1234" — The product number. Grouping and Capturing: Grouping and capturing allows you to collect specific parts of the text that match a pattern. These are especially useful when you need to extract data from a string. Grouping and Capturing: Regex: (\d{3}) e.g. Staff code Explanation: The parentheses () create a group, and \d{3} matches exactly three digits. Example Match: "123" This will match because "123" is exactly three digits, and the group captures the digits. Captured Group: "123" The matched digits are captured as a group. Regex: ([A-Za-z]{2})(\d{4}) Explanation: This pattern has two groups: eg a product SKU ([A-Za-z]{2}) captures two letters (e.g., the category). (\d{4}) captures four digits (e.g., the product number). Example Match: "AB1234" This will match because it starts with two letters, followed by four digits. Captured Group 1: "AB" — The category letters. Captured Group 2: "1234" — The product number. Regex: (\d{3}) e.g. Staff code Explanation: The parentheses () create a group, and \d{3} matches exactly three digits. Example Match: "123" This will match because "123" is exactly three digits, and the group captures the digits. Captured Group: "123" The matched digits are captured as a group. (\d{3}) Explanation: The parentheses () create a group, and \d{3} matches exactly three digits. Example Match: "123" This will match because "123" is exactly three digits, and the group captures the digits. Captured Group: "123" The matched digits are captured as a group. Explanation: The parentheses () create a group, and \d{3} matches exactly three digits. Explanation: () \d{3} Example Match: "123" This will match because "123" is exactly three digits, and the group captures the digits. Example Match: "123" This will match because "123" is exactly three digits, and the group captures the digits. This will match because "123" is exactly three digits, and the group captures the digits. "123" Captured Group: "123" The matched digits are captured as a group. Captured Group: "123" The matched digits are captured as a group. The matched digits are captured as a group. Regex: ([A-Za-z]{2})(\d{4}) Explanation: This pattern has two groups: eg a product SKU ([A-Za-z]{2}) captures two letters (e.g., the category). (\d{4}) captures four digits (e.g., the product number). Example Match: "AB1234" This will match because it starts with two letters, followed by four digits. Captured Group 1: "AB" — The category letters. Captured Group 2: "1234" — The product number. ([A-Za-z]{2})(\d{4}) Explanation: This pattern has two groups: eg a product SKU ([A-Za-z]{2}) captures two letters (e.g., the category). (\d{4}) captures four digits (e.g., the product number). Example Match: "AB1234" This will match because it starts with two letters, followed by four digits. Captured Group 1: "AB" — The category letters. Captured Group 2: "1234" — The product number. Explanation: This pattern has two groups: eg a product SKU ([A-Za-z]{2}) captures two letters (e.g., the category). (\d{4}) captures four digits (e.g., the product number). Explanation: ([A-Za-z]{2}) captures two letters (e.g., the category). (\d{4}) captures four digits (e.g., the product number). ([A-Za-z]{2}) captures two letters (e.g., the category). ([A-Za-z]{2}) (\d{4}) captures four digits (e.g., the product number). (\d{4}) Example Match: "AB1234" This will match because it starts with two letters, followed by four digits. Example Match: "AB1234" This will match because it starts with two letters, followed by four digits. This will match because it starts with two letters, followed by four digits. Captured Group 1: "AB" — The category letters. Captured Group 1: "AB" Captured Group 2: "1234" — The product number. Captured Group 2: "1234" Greedy vs. Lazy Quantifiers: Greedy Quantifier: .* tries to match as much text as possible. Lazy Quantifier: .*? tries to match as little text as possible. Example: Given the string "cat in a hat": c.*t will match "cat in a hat" in its entirety (greedy), because .* stretches to include everything up to the last 't'. c.*?t will match only "cat" (lazy), because .*? stops as soon as it finds a 't', resulting in the smallest possible match. Greedy vs. Lazy Quantifiers: Greedy vs. Lazy Quantifiers: Greedy Quantifier: .* tries to match as much text as possible. Lazy Quantifier: .*? tries to match as little text as possible. Greedy Quantifier: .* tries to match as much text as possible. .* Lazy Quantifier: .*? tries to match as little text as possible. .*? Example: Given the string "cat in a hat": Example: c.*t will match "cat in a hat" in its entirety (greedy), because .* stretches to include everything up to the last 't'. c.*?t will match only "cat" (lazy), because .*? stops as soon as it finds a 't', resulting in the smallest possible match. c.*t will match "cat in a hat" in its entirety (greedy), because .* stretches to include everything up to the last 't'. c.*t .* c.*?t will match only "cat" (lazy), because .*? stops as soon as it finds a 't', resulting in the smallest possible match. c.*?t .*? Important Regex Flags: Multiline and Global Flags modify how the regex engine interprets the pattern or processes the text. Flag Meaning Example Notes g Global (find all matches) /cat/g Without g, the engine stops after the first match. m Multiline /^R.*/m ^ and $ match the start/end of every line, not just the string. i Case-insensitive /cat/i Matches "Cat", "CAT", etc. s Dotall (single-line) /cat/s . matches newline characters as well. x Extended (verbose) mode Depends on flavor Allows whitespace/comments for readability (not in all flavors). Flag Meaning Example Notes g Global (find all matches) /cat/g Without g, the engine stops after the first match. m Multiline /^R.*/m ^ and $ match the start/end of every line, not just the string. i Case-insensitive /cat/i Matches "Cat", "CAT", etc. s Dotall (single-line) /cat/s . matches newline characters as well. x Extended (verbose) mode Depends on flavor Allows whitespace/comments for readability (not in all flavors). Flag Meaning Example Notes Flag Flag Meaning Meaning Example Example Notes Notes g Global (find all matches) /cat/g Without g, the engine stops after the first match. g g Global (find all matches) Global (find all matches) /cat/g /cat/g Without g, the engine stops after the first match. Without g, the engine stops after the first match. m Multiline /^R.*/m ^ and $ match the start/end of every line, not just the string. m m Multiline Multiline /^R.*/m /^R.*/m ^ and $ match the start/end of every line, not just the string. ^ and $ match the start/end of every line, not just the string. i Case-insensitive /cat/i Matches "Cat", "CAT", etc. i i Case-insensitive Case-insensitive /cat/i /cat/i Matches "Cat", "CAT", etc. Matches "Cat", "CAT", etc. s Dotall (single-line) /cat/s . matches newline characters as well. s s Dotall (single-line) Dotall (single-line) /cat/s /cat/s . matches newline characters as well. . matches newline characters as well. x Extended (verbose) mode Depends on flavor Allows whitespace/comments for readability (not in all flavors). x x Extended (verbose) mode Extended (verbose) mode Depends on flavor Depends on flavor Allows whitespace/comments for readability (not in all flavors). Allows whitespace/comments for readability (not in all flavors). Note: Not all regex flavors support all flags. For example, x is common in some languages (like Perl or PCRE) but not in others. Always check your language-specific documentation. For JavaScript, the commonly available flags are g, m, i, s, and u. Note: Applying Flags in Examples Multiline Flag (m): If you have: Hello world Regex is fun Using ^Regex without /m won’t match because "Regex" doesn’t appear at the start of the entire string. With /m, ^Regex matches the start of the second line therefore it’ll match. Global Flag (g): Given "banana", /a/ finds only the first 'a', but /a/g finds all three 'a's. Multiline Flag (m): If you have: Hello world Regex is fun Using ^Regex without /m won’t match because "Regex" doesn’t appear at the start of the entire string. With /m, ^Regex matches the start of the second line therefore it’ll match. Multiline Flag ( m ): If you have: Multiline Flag ( m ): Hello world Regex is fun Hello world Regex is fun Using ^Regex without /m won’t match because "Regex" doesn’t appear at the start of the entire string. With /m , ^Regex matches the start of the second line therefore it’ll match. ^Regex /m /m ^Regex Global Flag (g): Given "banana", /a/ finds only the first 'a', but /a/g finds all three 'a's. Global Flag ( g ): Given "banana" , /a/ finds only the first 'a', but /a/g finds all three 'a's. Global Flag ( g ): "banana" /a/ /a/g Real-World Examples Matching a Twitter Handle Problem: Match a Twitter handle. A typical Twitter handle: Problem: Starts with @ Followed by letters, digits, or underscore Up to 15 characters long Starts with @ @ Followed by letters, digits, or underscore Up to 15 characters long Regex: ^@[A-Za-z0-9_]{1,15}$ ^@[A-Za-z0-9_]{1,15}$ Explanation: Explanation: ^ and $ ensure you match the entire string. @ matches the literal “@” symbol. [A-Za-z0-9_] allows letters, digits, and underscore. {1,15} limits length to 1–15 characters. ^ and $ ensure you match the entire string. ^ $ @ matches the literal “@” symbol. @ [A-Za-z0-9_] allows letters, digits, and underscore. [A-Za-z0-9_] {1,15} limits length to 1–15 characters. {1,15} Examples: Examples: Matches: @john_doe, @User123 Does not match: john_doe (no @), @thisUsernameIsTooLongForTwitterHandles Matches: @john_doe , @User123 @john_doe @User123 Does not match: john_doe (no @ ), @thisUsernameIsTooLongForTwitterHandles john_doe @ @thisUsernameIsTooLongForTwitterHandles Matching an Instagram Handle Problem: Instagram handles: Problem: Starts with @ Start and contain letters, numbers, underscores, and periods Up to 30 characters Starts with @ @ Start and contain letters, numbers, underscores, and periods Up to 30 characters Regex: ^@[A-Za-z0-9_.]{1,30}$ Regex: ^@[A-Za-z0-9_.]{1,30}$ Explanation: Explanation: ^ and $ for full string match. @ matches the literal “@” symbol. [A-Za-z0-9_.] allows letters, digits, underscore, and period. {1,30} sets length limit. ^ and $ for full string match. ^ $ @ matches the literal “@” symbol. @ [A-Za-z0-9_.] allows letters, digits, underscore, and period. [A-Za-z0-9_.] {1,30} sets length limit. {1,30} Examples: Examples: Matches: @jane.doe, @user_123, @_insta_guy Does not match: insta (no @), jane%doe (invalid % character) Matches: @jane.doe , @user_123 , @_insta_guy @jane.doe @user_123 @_insta_guy Does not match: insta (no @ ), jane%doe (invalid % character) insta @ jane%doe % A Simple Email Validator Problem: Basic email pattern: <username>@<domain>.<tld> Problem: <username>@<domain>.<tld> Regex: ^[A-Za-z0-9._%+\-]+@[A-Za-z0-9.\-]+\.[A-Za-z]{2,}$ Regex: ^[A-Za-z0-9._%+\-]+@[A-Za-z0-9.\-]+\.[A-Za-z]{2,}$ Explanation: Explanation: [A-Za-z0-9._%+\-]+: Username with allowed characters (., _, %, +, -). @: Literal “@”. [A-Za-z0-9.\-]+: Domain name with letters, digits, dots, and hyphens. \.[A-Za-z]{2,}: A literal dot followed by at least two letters. [A-Za-z0-9._%+\-]+ : Username with allowed characters ( . , _ , % , + , - ). [A-Za-z0-9._%+\-]+ . _ % + - @ : Literal “@”. @ [A-Za-z0-9.\-]+ : Domain name with letters, digits, dots, and hyphens. [A-Za-z0-9.\-]+ \.[A-Za-z]{2,} : A literal dot followed by at least two letters. \.[A-Za-z]{2,} Examples: Examples: Matches: jane.doe@example.com, user_123@my-site.org Does not match: jane.doe@, @domain.com, user@domain Matches: jane.doe@example.com , user_123@my-site.org jane.doe@example.com user_123@my-site.org Does not match: jane.doe@ , @domain.com , user@domain jane.doe@ @domain.com user@domain Real-world email validation is more complex, but this is a good starting point. Real-world email validation is more complex, but this is a good starting point. Tips for Improving Your Regex Skills Start Simple, Then Build Up: Begin with small patterns and add complexity as you understand each part. Use Comments and Verbose Mode (If Available): Some regex flavors allow a verbose mode (e.g., the x flag in some languages) where you can space out your pattern and add comments. This is invaluable for maintaining complex regexes. Test on Multiple Examples: Use positive (should match), negative (should not match), and edge-case examples to ensure your regex works as intended. Learn Common Patterns: Familiarize yourself with frequently needed patterns (such as email addresses, phone numbers, and URLs). Having a library of known patterns saves time. Study Documentation and Reference Guides: Keep a reference handy. Tools like Regular-Expressions.info offer comprehensive tutorials and references. Practice, Practice, Practice: The more you use regex, the more intuitive it becomes. Challenge yourself by solving common text parsing or validation problems. Start Simple, Then Build Up: Begin with small patterns and add complexity as you understand each part. Start Simple, Then Build Up: Use Comments and Verbose Mode (If Available): Some regex flavors allow a verbose mode (e.g., the x flag in some languages) where you can space out your pattern and add comments. This is invaluable for maintaining complex regexes. Use Comments and Verbose Mode (If Available): x Test on Multiple Examples: Use positive (should match), negative (should not match), and edge-case examples to ensure your regex works as intended. Test on Multiple Examples: Learn Common Patterns: Familiarize yourself with frequently needed patterns (such as email addresses, phone numbers, and URLs). Having a library of known patterns saves time. Learn Common Patterns: Study Documentation and Reference Guides: Keep a reference handy. Tools like Regular-Expressions.info offer comprehensive tutorials and references. Study Documentation and Reference Guides: Practice, Practice, Practice: The more you use regex, the more intuitive it becomes. Challenge yourself by solving common text parsing or validation problems. Practice, Practice, Practice: Conclusion Regular expressions might seem daunting, but starting with the basics and gradually exploring more complex patterns and flags will help you master this powerful tool. Use online tools, consult documentation for your environment, and practice regularly. Soon, you’ll go from avoiding regex to confidently using it as one of your essential tools. Happy matching! Happy matching!