Explaination about Shorthand character sets.mp4



Download Explanation about Regex shortnand character sets.mp4

 Here’s a simple and clear explanation of the most common Regex shorthand character sets, with easy examples to help you understand how they work.


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: 3 in "A3Z"

  • Regex: \d\d
    Matches: "45" in "Age: 45"


๐Ÿ”น 2. \D → Non-digit characters

Meaning: Matches anything except 0–9

Examples:

  • Regex: \D
    Matches: A in "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: a in "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: H in " 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

How to Match decimal numbers and currency in Regex.mp4

  Download  How to Match decimal numbers and currency in Regex.mp4 Here’s a simple and easy explanation of how to match decimal numbers and...