Fixed Top Ad (Local Preview)800x90 • Slot 6608427872

Regex Concepts

1. Introduction to Regex

    1.1 🔎 What is Regex?
    1. Regex means Regular Expression. Regex is used to search, validate, extract, and replace text based on patterns. It is useful for: 👉 Searching text 👉 Validating email or phone numbers 👉 Extracting information from logs 👉 Cleaning messy text data 👉 Finding patterns in large datasets 📌 Regex is a powerful search tool that searches by pattern, not just exact words. 📌 Imagine describing a phone number as "any 10 digits" instead of one exact number.
Concept on Regex
Regex Concept
    1.2 🧹 Why Learn Regex?
    1. Regex is very useful in Data Science because real-world data is often messy. Regex is available in many tools: ✅ Text editors ✅ Excel-like tools ✅ SQL ✅ Python ✅ JavaScript ✅ Online regex testers For practice, we will use this Regex Tester tool.
    1.3 ⚠️ Why Regex Looks Difficult
    1. Regex syntax can look confusing at first. Example: \d{10} But this simply means: Match exactly 10 digits. 📌 Regex looks difficult because it uses symbols, but each symbol has a small meaning.

2. Literals

    2.1 🔤 What are Literals?
    1. Literals are normal characters that match exactly as written. Example: catText: The cat is on the roof. Match: cat
    2.2 Special Characters
    1. Some characters have special meanings in regex. Common special characters:. ^ $ * + ? { } [ ] | ( ) \ If we want to match them as normal text, we must escape them using backslash \. Example: This matches the dollar sign: \$ Normal letters match normally. Special characters need \.
Literal Matching Examples
TextDesired MatchRegex Pattern
The cat is on the roof.catcat
The price is $100.$100\$100
I have 10+ years of experience.10+10\+
Today is 2026.06.15 or 2026/06/152026.06.152026\.06\.15

3. ⚡ Special Regex Characters

3.1 Common Special Patterns
PatternMeaning
\nNewline
\tTab
\dAny digit, same as [0-9]
\DAny non-digit
\wWord character, same as [a-zA-Z0-9_]
\WNon-word character
\sWhitespace, such as space or tab
\SNon-whitespace
\bWord boundary
\BNon-word boundary
.Any single character except newline
\Escapes special characters
^Start of string
$End of string
    3.2 Examples
    1. Match digits Text: I bought 100 apples. Regex: \d+ Match: 100Match words Text: Hello_123 Regex: \w+ Match: Hello_123Match whitespace Text: Hello World Regex: \s Match: space between Hello and World 📌 Special regex shortcuts help us match common character types quickly.

4. Character Classes

    4.1 🔡 What is a Character Class?
    1. A character class lets us match one character from a set. Syntax: [abc] This matches: a or b or c
Character Class
Character Class
4.2 Character Class Examples
PatternMeaningExample Match
[aeiou]Any vowela in cat, e in bed
[A-Z]Any uppercase letterH in Hello
[0-9]Any digit5 in 5kg
\dAny digit5 in 5kg
[^0-9]Any non-digitA in A1
    4.3 Ranges
    1. We can use ranges inside character classes. - [a-z] matches any lowercase letter. - [A-Z] matches any uppercase letter. - [0-9] matches any digit.
    4.4 Negation
    1. Inside a character class, ^ means not when written at the beginning. [^0-9] This means: Match anything that is not a digit. 📌 Character classes help us say "match one character from this group."

5. Groups

    5.1 🧩 What are Groups?
    1. Groups combine multiple characters or patterns together. Groups are useful when we want to: - Reuse part of a pattern - Capture part of matched text - Apply quantifiers to multiple characters - Use alternatives clearly
    5.2 Capturing Groups
    1. Capturing groups use parentheses: (...) Text matched inside parentheses can be referenced later. Example: (\d{4})([-./])(\d{2})\2(\d{2}) This can match dates like: - 2026-06-15 - 2026.06.15 - 2026/06/15 Here: (\d{4}) captures year ([-./]) captures separator \2 means reuse the second captured group 📌 Simple idea: Capturing groups remember matched parts.
    5.3 Capturing Group Example: Website
    1. https?://(www\.)?(\w+)(\.\w+)Example text: https://www.google.comPossible captured groups: - \1 captures www. - \2 captures google - \3 captures .com
Capturing vs Non-Capturing Group
Capturing vs Non-Capturing Group
    5.4 Non-Capturing Groups
    1. Non-capturing groups use (?:...). They group a pattern but do not store it for back-referencing. Example: (?:Mr|Mrs|Ms)\.?\s[A-Za-z]+ Matches: ✅ Mr Ram ✅ Mrs Smith ✅ Ms Davis But it does not capture the title as a numbered group. 📌 Use (...) when you want to capture. 📌 Use (?:...) when you only want to group.

6. 🔀 Alternation

    6.1 🔀 What is Alternation?
    1. Alternation means OR. It uses | to match one pattern or another.
6.2 Alternation Examples
PatternMeaningExample Match
cat|dogMatch cat or dogcat, dog
\b(cat|dog)\bMatch cat or dog as whole wordscat, dog
(Mr|Mrs|Ms)\s[A-Za-z]+Match title and nameMr John, Mrs Smith
Alternation
Alternation
    6.3 Why Use Groups with Alternation?
    1. Without grouping: Mr|Mrs|Ms John This means: Mr OR Mrs OR Ms John Better: (Mr|Mrs|Ms)\sJohn This means: Mr John OR Mrs John OR Ms John 📌 Simple idea: Use | for OR. Use groups to control what OR applies to.

7. 🔢 Quantifiers

    7.1 🔢 What are Quantifiers?
    1. Quantifiers tell regex how many times the previous pattern should appear. Sample text: I bought 100 apples and 50 oranges.
7.2 Common Quantifiers
QuantifierMeaningRegexMatch
*0 or more\d*empty match, 100, 50
+1 or more\d+100, 50
?0 or 1\d?empty match, 1, 0, 5
{n}Exactly n times\d{3}100
{n,}n or more times\d{2,}100, 50
{m,n}Between m and n times\d{1,3}100, 50
Quantifiers
Quantifiers
    7.3 Useful Quantifier Examples
    1. Match a 10-digit phone number: \d{10} Matches: 9841000000 Match one or more letters: [a-zA-Z]+Match optional s: apples? Matches: apple, apples 📌 Quantifiers control "how many."

8. 🍽️ Greedy vs Lazy Quantifiers

    8.1 🍽️ Greedy Quantifiers
    1. A greedy quantifier tries to match as much text as possible. Sample text: <div>Content</div><div>More</div>Pattern: <div>.*</div>Match: <div>Content</div><div>More</div> It matches too much because .* is greedy.
    8.2 🐜 Lazy Quantifiers
    1. A lazy quantifier tries to match as little text as possible. We make a quantifier lazy by adding ?. Pattern: <div>.*?</div> Matches: <div>Content</div> <div>More</div>
Greedy vs Lazy Quantifiers
Greedy vs Lazy Quantifiers
8.3 Greedy vs Lazy Comparison
TypeRegexMatch
Greedy<div>.*</div><div>Content</div><div>More</div>
Lazy<div>.*?</div><div>Content</div> and <div>More</div>

9. 👀 Lookaround

    9.1 👀 What is Lookaround?
    1. Lookaround checks what comes before or after a pattern without including it in the match. There are two main types: 👉 Lookahead: checks after 👉 Lookbehind: checks before

10. 👁️➡️ Lookahead

    10.1 Positive Lookahead
    1. Syntax: (?=...) It checks if a pattern appears after the current position. Example: Text: Weight: 50kg and 20m Regex: \d+(?=kg) Match: 50 It matches 50 only because it is followed by kg.
    10.2 Negative Lookahead
    1. Syntax: (?!...) It checks that a pattern does not appear after the current position. Example: Text: Weight: 50kg, Count: 20 items Regex: \d+\b(?!kg) Possible match: 20 📌 Lookahead checks what comes after, but does not include it in the match.

11. 👁️⬅️ Lookbehind

    11.1 Positive Lookbehind
    1. Syntax: (?<=...) It checks if a pattern appears before the current position. Example: Text: Price: $100 or Rs 15000 Regex: (?<=\$)\d+ Match: 100 It matches 100 only because it is preceded by $.
    11.2 Negative Lookbehind
    1. Syntax: (?<!...) It checks that a pattern does not appear before the current position. Example: Text: Price: $100, Quantity: 20 Regex: (?<!\$)\b\d+\b Possible match: 20 📌 Lookbehind checks what comes before, but does not include it in the match.
Lookaround
Lookaround
Lookaround Examples
PatternMeaningExample Match
(?<=Mr\. )\w+Match name after Mr.Ram in Mr. Ram
\w+(?=:\s)Match word followed by colon and spaceJohn in John: Doe
(?<!hot)dogMatch dog not preceded by hotdog in The dog ran away.
\d+(?=kg)Match digits followed by kg50 in Weight: 50kg
(?<=@)\w+(?=\.(com|edu))Match domain after @ and before .com or .eduuniversity in [email protected]

12. ⚓ Anchors

    12.1 ⚓ What are Anchors?
    1. Anchors are used to match the start or end of a string.
Anchors
AnchorMeaning
^Start of string
$End of string
    12.2 Anchor Examples
    1. Sample text: The cat is on the roof.
PatternMeaningExample Match
^TheMatch The at the startThe
roof\.$Match roof. at the endroof.
^The.*\.$Match full string starting with The and ending with periodThe cat is on the roof.

13. 🎯 Practical Regex Examples

    13.1 Match Email-like Text
    1. [\w.-]+@[\w.-]+\.\w+Example match: [email protected]
    13.2 Match Nepali Phone Number Style
    1. 9[78]\d{8}Example match: 9841000000
    13.3 Match Date Format YYYY-MM-DD
    1. \d{4}-\d{2}-\d{2}Example match: 2026-07-16
    13.4 Match Price
    1. \$\d+Example match: $100
Ad PlaceholderSlot: 7421026683

Practice QuestionsNot started

  1. Basic Regex Practice

    Question 1 of 2

    • Write regex to match the following patterns: - cat in any sentence. - Digit in a sentence - Lowercase letter in a sentence - Five letter word in a sentence. - Word that starts with "a" and ends with "e". - Word ending with ing.
  2. Intermediate Regex

    Question 2 of 2

    • Write regex to match the following patterns: - Email address in a text. - Date in formats YYYY-MM-DD, YYYY/MM/DD, YYYYMMDD or YYYY.MM.DD. - Word that ends with ing. Only ensure but don't consider in match - Weights that have kg at end. Ensure kg but match weight only - Name of tags in the html document of dataset. E.g.: div, br, p - Website Name from url like: https://www.google.com, www.youtube.com - Hashtag in a social media post. - Phone number in formats: (+977) 456-7890, 4567890, 456-3456. - Mobile Number in formats: 9843468713 or 984-3468-713.
Ad PlaceholderSlot: 5413242224