Module 2: Quantifiers in Reg Ex for Beginners
Quantifiers are essential for both understanding and using regular expression effectively. It is a critical feature of Regular Expressions. It allows you to do many things. Quantifiers specify how many instances of character group or character class present in the input for a match to be found. It means it allows you to repeat characters, metacharacters, and an entire subexpression in the Regex. If you don’t know exactly how many characters are coming, you can use quantifiers.
There are four different syntaxes available to indicate the repetition in Regular Expressions. The first three are single character quantifiers. The first quantifier is the “plus” sign and what it allows you to do is to match the previous character one or more times. There is no limit on how many times it can match. This can be used when you want to find at least one instance and you want to make sure that it's there, but you don't care how many times it repeats. We will take a closer look at that in the Demo.
The next metacharacter is the “star”, which is used to match zero or more instances of the previous character. You do have to be careful when using this meta character because it does allow zero instances of a match to be found, which makes it optional and it will match anything. The only difference between the star and the plus sign is the plus requires at least one match to be found. Otherwise, it will fail to match.
Now, the next three meta character sequences allow you to specify the number of matches by providing a minimum or maximum value. It provides us with more precision as it allows us to indicate the exact quantity we want to match.
So to specify the exact number of matches, you would place them inside to calibrate which matches the previous element exactly n times. Here n is a non-negative number. The next is say, if you want to match something, at least n number of times, then what you need to do is in calibrates, specify the number followed by a comma, and then closing the calibrates, which indicates match something minimum n times.
The very last quantifier that I have listed here, which allows you to specify a minimum and a maximum number of matches to be found. This expression would find a match where there is at least an N character or minimum N character, but not more than M characters. Quantifiers are extremely useful meta characters, but the downside of this expression are they can get complex very fast.
Look at the simple Regex. It simply matches the valid email address. But look at this crazy expression that you found on the Internet. I think this is why people stay away from the regular expression because it appears very complex at first if you don't have any fundamental knowledge of any of the meta characters, but luckily you are not because you are watching this course.
Now, let us move on to the demo so you can learn how to use these quantifiers in your expression.
This is one of the great regular expression testing tools provided by BYTESCOUT. In this demo, we will see the power of + quantifier or star quantifier. Star matches zero or more characters, plus quantifier matches one or more characters. Here in the input text, I have defined this word now to match this word I will use the dot metacharacter. The period or dot meta-character that will match anything except the new line character.
Here to match the whole world, I need to repeat it nine times so that it will match the whole world. As you can see that it is now matching the entire string. But wouldn't it be great if I didn't have to repeat that period nine times? Luckily, that's exactly when you would want to use quantifiers. For example, if I remove it and add the star or plus quantifier over here, then we will get the same output.
We reduced overexpression from nine characters to two characters. It is dangerous to use period or dot because you are matching anything zero or more times. It will match absolutely nothing. You have to be careful when you use the period which matches anything with a quantifier.
Now let's take a look at another demo. There are three words: user, users, and users with three S. I want to match only the top two words, user and users. It means I want to make this optional. Write the Regex to match the first two words or to get the first two words. If I write users over here, then it will give me the last two words. But I want to get the first two words. To get the first two words here, I need to make this optional.
To make it optional, I need to add a question mark quantifier. Once I add the question mark quantifier, it will start matching all these three words. End the anchor over here. Now I'm getting the expected output, which I want. As you can see from the output, the one with the three S is not returning. This is how you can use the question mark quantifier.
Let's move on to some other example. Here in the input string, I have added one sentence. In this sentence, I want to match the word which ends with the ut character. Write Regex to get those two words, if I define the character range with a plus quantifier, then it starts giving me all the words from this sentence. But I want to find the word which ends with the ut character. Now write the character. It will start giving me the expected output, which we want.
Let us move on to some practical examples of quantifiers. Here in the input text, I have defined the possible format of phone numbers. Write the Regex step by step to match this phone number. But first, analyze the pattern. Here this round bracket is optional. This phone number may contain a hyphen or it may contain a dot or it may contain a space.
First, we will write the regex for this pattern(123) and then after this pattern(456), and then after this pattern(6789). This number is followed either by a hyphen or dot and space. We will also handle this scenario in Regex. Write the Regex for this pattern. As I say this round bracket is optional. First, define the round bracket to make it optional. I can either use a ranged character like this (0,1) or I can use question mark characters and then we will define the numerical range, which is (0-9).
But I want to match only three digits. I will add that length followed by the closing round bracket and this round bracket is also optional. Add the question mark quantifier over here. This phone number either contains a hyphen, dot, or space. Add that character in the character class hyphen, dot, and space, now we will match the next three digits. For that, add the numerical range (0-9). After this number, there may be, again, hyphen space or dot, and write the regex for four digits. It started matching the phone number as expected.
Now walking through what this expression is meaning. It starts with the round bracket meta character, followed by a question mark because a round bracket may be optional over here. It does the same thing for the closing round bracket.
The next part of the expression is a number range meta character looking for exactly three digits. This is going to match one, two, three, part of the number after those three digits, it's looking for either hyphen or space or dot and then another three digits, which matches four, five, six.
Right after the second set of three digits, there is another hyphen or space and dot then another number range looking for exactly four digits. This part of the expression matches six, seven, eight, nine.
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