Module 2: Regex Syntax in Detail (Part 2)
Previous Tutorial - Next Tutorial
What happens if we want to match a special character from within a character class, for example, the carrot sign, dollar sign or the period dot or the opening square bracket or backslash, which is also called metacharacters. The simplest way to make this special character is to escape them by simply adding the backslash before that character.
Character ranges generally follow the character order of the local. Here what I mean to say is, let's look at this ASCII character table. As you can see, if you use capital A to capital P as the range, all is fine. The correct group of characters is selected. However, if I change the range to go from capital A to lowercase D, the characters between the capital Z in the lower case E are also selected. Obviously, this is just a simple example, but be careful when you define your character ranges in your regular expression using this character class.
Another thing to be wary of is the order in which you give the characters to define a range. For example, if I define my range as zero to nine, then no problem as it is a valid range. However, if I define the ranges as nine to zero, the behavior is not completely defined. That means your Regex is considered either invalid or your Regex engine might throw an error. So be careful when you define the character order within the Regex.
In some of the card games the Joker is a wild card and used to represent any other playing card, but with certain restrictions. Same way in regular expression there is a concept of wild card which is represented by the dot metacharacter. The dot will generally match anything like alphabetic or numeric character or whitespace punctuations and any other symbols except the new line character. When using a dot within a character class, it loses its special meaning and matches just a little dot. Here this [a.c] matches only either A or Dot or C, but if I remove this character class, then it matches ABC or ADC, etc.
Using a dot within a character class as a wildcard would be kind of senseless anyway as it already matches everything. This is the end of part one of module two. In the next part, we will learn about quantifiers, metacharacter alternation, sub-pattern, and grouping.
Web API for developers Free Trial Offline SDK
Here's RegEx video tutorial:
Other useful articles:
- How to Use RegEx for Data Extraction
- How to Find Total Tax Using a Regular Expression in C#
- How to Find a Number Using Regular Expressions in C#
- How to Find Invoice Numbers Using Regular Expressions in C#
- Find SSN Using a Regular Expression in C#
- Find Total Amount Using a Regular Expression in C#
- How to Find Website Links using Regex
- Module 1: Regular Expressions for Beginners
- Module 1: Regex Usage and Tool Demo
- Module 2: Regex Engine Basics (Part 1)
- Module 2: Regex Engine Basics (Part 2)
- Module 2: Regex Syntax in Detail (Part 1)
- Module 2: Regex Syntax in Detail (Part 2)
- Module 2: Quantifiers in Reg Ex for Beginners
- Module 2: Short Codes in Reg Ex for Beginners
- Module 2: Anchors and Boundaries in Detail
- Module 2: Grouping and Subpattern in Detail
- Module 3: Realtime Use Case of Regular Expressions - Part 1
- Module 3: Realtime Use Case of Regular Expressions - Part 2
- Module 3: Realtime Use Case of Regular Expressions - Part 3
- Module 3: Realtime Use Case of Regular Expressions - Part 4
- How to Find Quantity Field Using Regular Expression in C#
- How to Find Phone Numbers without a Specific Format
- How to Find Date Using Regular Expression in C#
- How to Find Time Using Regular Expression in C#
- How to Find a Sentence Using Regular Expressions in C#
- Find a Word Using Regular Expression in C#
- Find a Due Date using Regular Expressions in C#
- How to Find the End of a String Using Regular Expression in C
- How to Find the Start of a String Using Regular Expression in C
- How to Find a Comma using Regular Expression in C Sharp
- How to Find a Dot using Regular Expression in C
- How to Find a Semicolon using Regular Expression in C Sharp
- How to Find a Double Space using Regular Expression in C