Download Explanation about Regex shortnand character sets.mp4
✅ Regex Shorthand Character Sets (Explained Simply)
Regular expressions (regex) use shorthand character sets to make pattern-matching easier.
Instead of writing long character ranges, you can use short symbols like \d, \w, \s, etc.
Below is a friendly explanation of each.
๐น 1. \d → Digit characters
Meaning: Matches any number from 0–9
Examples:
-
Regex:
\d
Matches:3in"A3Z" -
Regex:
\d\d
Matches:"45"in"Age: 45"
๐น 2. \D → Non-digit characters
Meaning: Matches anything except 0–9
Examples:
-
Regex:
\D
Matches:Ain"A3" -
Regex:
\D+
Matches:"Name"in"Name123"
๐น 3. \w → Word characters
Meaning: Matches
-
Letters (A–Z, a–z)
-
Numbers (0–9)
-
Underscore (_)
Examples:
-
Regex:
\w
Matches:ain"a!" -
Regex:
\w+
Matches:"Hello123"in"Hello123!!"
๐น 4. \W → Non-word characters
Meaning: Matches anything except letters, numbers, and underscore
Examples:
-
Regex:
\W
Matches:!in"Hi!" -
Regex:
\W+
Matches:" @#"in"User @# Name"
๐น 5. \s → Whitespace characters
Meaning: Matches spaces, tabs, and newlines
(space, \t, \n)
Examples:
-
Regex:
\s
Matches the space in"Hello World" -
Regex:
\s+
Matches" "(multiple spaces)
๐น 6. \S → Non-whitespace characters
Meaning: Matches anything that is not space, tab, or newline
Examples:
-
Regex:
\S
Matches:Hin" Hello" -
Regex:
\S+
Matches:"Hello"in"Hello World"
๐ Additional Character Class Shorthands (POSIX Style)
Some regex engines (like Linux grep -E, PHP, Perl) use POSIX character classes inside [[: ... :]].
Examples:
๐น [:digit:] → Digits (0–9)
๐น [:alpha:] → Alphabet letters (A–Z, a–z)
๐น [:alnum:] → Letters + numbers
๐น [:upper:] → Uppercase letters
๐น [:lower:] → Lowercase letters
Example use:
[[:digit:]]+
Matches "2024" in "Year: 2024"
๐ฏ Summary Table (Quick View)
| Shorthand | Meaning | Example Match |
|---|---|---|
\d |
Digit | 5 |
\D |
Not a digit | A |
\w |
Word character | A, 3, _ |
\W |
Not word character | ! |
\s |
Whitespace | (space) |
\S |
Non-whitespace | H |
[[:digit:]] |
Digit | 7 |
[[:alpha:]] |
Letters | A, z |
Enjoy! Follow us for more...


No comments:
Post a Comment