VibeKoding / Ensiklopedia ยท Fondasi KuatEnsiklopedia ยท Fondasi Kuat / Fundamentals of Regular ExpressionsFundamentals of Regular Expressions
VK

Fundamentals of Regular ExpressionsFundamentals of Regular Expressions

๐Ÿ“š Ensiklopedia ยท Fondasi KuatEnsiklopedia ยท Fondasi Kuat ๐ŸŒ Dual Bahasa (ID / EN) โšก VibeKoding Native

Ensiklopedia VibeKoding: Fundamentals of Regular Expressions.Ensiklopedia VibeKoding: Fundamentals of Regular Expressions.

> ๐Ÿ’ก Learning Guide: Regular expressions look like gibberish? They're actually just a mini "language for describing text patterns." This chapter takes you from zero to understanding the core ideas of regex, and teaches you to solve 80% of text search and validation problems with just a few key symbols.> ๐Ÿ’ก Learning Guide: Regular expressions look like gibberish? They're actually just a mini "language for describing text patterns." This chapter takes you from zero to understanding the core ideas of regex, and teaches you to solve 80% of text search and validation problems with just a few key symbols.

1. Regex Quick Start: Up and Running in Three Minutes1. Regex Quick Start: Up and Running in Three Minutes

๐Ÿ‘‡ Try it out: Enter a regular expression and see matching results in real time.๐Ÿ‘‡ Try it out: Enter a regular expression and see matching results in real time.

๐Ÿ’ก Tips Praktis๐Ÿ’ก Pro Tip

Regular expressions = using special symbols to describe "what kind of text you want to find." \d represents digits, + represents one or more, so \d+ means "one or more digits."Regular expressions = using special symbols to describe "what kind of text you want to find." \d represents digits, + represents one or more, so \d+ means "one or more digits."

------

2. Core Concepts: Building Blocks You Combine2. Core Concepts: Building Blocks You Combine

The essence of regex is combining three types of building blocks to create the pattern you want:The essence of regex is combining three types of building blocks to create the pattern you want:

2.1 Block One: Character Classes (What Characters to Match)2.1 Block One: Character Classes (What Characters to Match)

SyntaxMeaningExample
.Any charactera.c โ†’ abc, a1c, a c
\dDigit [0-9]\d\d โ†’ 42, 99
\wLetter/digit/underscore\w+ โ†’ hello, user_1
\sWhitespace characterMatches spaces, tabs
[abc]Any one in the set[aeiou] โ†’ vowels
[^abc]Not in the set[^0-9] โ†’ non-digit characters

2.2 Block Two: Quantifiers (How Many Times to Match)2.2 Block Two: Quantifiers (How Many Times to Match)

SyntaxMeaningExample
*0 or more timesab* โ†’ a, ab, abbb
+1 or more timesab+ โ†’ ab, abbb (doesn't match a)
?0 or 1 timecolou?r โ†’ color, colour
{3}Exactly 3 times\d{3} โ†’ 123
{2,4}2 to 4 times\d{2,4} โ†’ 12, 1234

2.3 Block Three: Position and Grouping2.3 Block Three: Position and Grouping

SyntaxMeaningExample
^Start of line^Hello โ†’ lines starting with Hello
$End of lineend$ โ†’ lines ending with end
\bWord boundary\bcat\b โ†’ cat (doesn't match catch)
(...)Capturing group(\d+)-(\d+) โ†’ captures separately
`a\b`Or`cat\dog` โ†’ cat or dog

------

3. Practice: Common Validation Patterns3. Practice: Common Validation Patterns

3.1 Email Validation3.1 Email Validation

CODE
[\w.+-]+@[\w-]+\.[\w.]+

Breakdown:Breakdown:

3.2 Phone Number Validation (China)3.2 Phone Number Validation (China)

CODE
1[3-9]\d{9}

Breakdown:Breakdown:

3.3 Password Strength Check3.3 Password Strength Check

CODE
^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).{8,}$

Breakdown:Breakdown:

------

4. Using Regular Expressions in Code4. Using Regular Expressions in Code

JavaScriptJavaScript

javascript
const text = 'Contact: 13812345678 or 15099887766' const regex = /1[3-9]\d{9}/g const phones = text.match(regex) // ['13812345678', '15099887766'] // Replace text.replace(/\d{4}(?=\d{4}$)/, '****') // Hide the middle four digits of the phone number // Validate /^[\w.+-]+@[\w-]+\.[\w.]+$/.test('user@example.com') // true

PythonPython

python
import re text = 'Price is 99 yuan, discount 20 yuan' numbers = re.findall(r'\d+', text) # ['99', '20'] # Replace re.sub(r'\d+', 'X', text) # 'Price is X yuan, discount X yuan' # Group capture match = re.search(r'(\d+)-(\d+)', '2024-01-15') match.group(1) # '2024' match.group(2) # '01'

------

5. Greedy vs. Lazy: A Key Difference5. Greedy vs. Lazy: A Key Difference

CODE
Text: <b>hello</b> and <b>world</b>
PatternMatch ResultExplanation
.*hello and worldGreedy: match as much as possible
.*?helloLazy: match as little as possible
๐Ÿ’ก Tips Praktis๐Ÿ’ก Pro Tip

The default is greedy mode. Add ? after a quantifier to switch to lazy mode. Most of the time, you want lazy mode.The default is greedy mode. Add ? after a quantifier to switch to lazy mode. Most of the time, you want lazy mode.

------

6. Summary6. Summary

๐Ÿ’ก Tips Praktis๐Ÿ’ก Pro Tip

1. Regex = a mini language for describing text patterns, used for searching, matching, and replacing 2. Three types of building blocks: character classes (what to match) + quantifiers (how many times) + position/grouping 3. \d \w \s are the three most commonly used character classes, covering digits, word characters, and whitespace 4. No need to write from scratch: common scenarios have mature regex patterns you can reuse 5. Greedy vs. Lazy: default is greedy (match more), add ? for lazy (match less)1. Regex = a mini language for describing text patterns, used for searching, matching, and replacing 2. Three types of building blocks: character classes (what to match) + quantifiers (how many times) + position/grouping 3. \d \w \s are the three most commonly used character classes, covering digits, word characters, and whitespace 4. No need to write from scratch: common scenarios have mature regex patterns you can reuse 5. Greedy vs. Lazy: default is greedy (match more), add ? for lazy (match less)

Next Steps:Next Steps: