Module 3: Realtime Use Case of Regular Expressions for Beginners - Part 2
Previous Tutorial - Next Tutorial
Validating credit card numbers using regular expressions is one of the common usages. On an actual credit card, the digits are usually placed into groups of four, which makes the credit card easier for humans to read. Almost all of the credit card companies use this number format. This credit card number consists of 16 digits and these digits are divided into four groups and each group consists of four digits.
So without thinking much, let's write the regex to match this format. Here I have added some sample credit card numbers, followed by either hyphen or space. In the first set, I want to match a digit exactly four times. First, define the meta character carrot sign to assert the beginning of the string and then backslash d four, followed by hyphen or space. Use the character class to define a hyphen and space. Now I want to repeat this expression to match the next two sets. For that group, this expression into parentheses and then use a repeating quantifier.
Lastly, we want to repeat the numeric pattern one last time, but now without the hyphen or space. Use backslash d and assert the end of the string. Here, as you can see that this last credit card number is not matching because that's an invalid credit card number. This is how we have a working regex to match credit card numbers.
Let's move to the next demo. In this demo, we will create a regex to match the simple date format, which is of (dd/mm/yyyy). You might think that date validation should be an easy job for the regular expression, right? No, it is not. The main issue is that regular expression does not directly deal with the numbers. I mean, we cannot tell a regular expression in a straightforward way to match a number between 1 to 31.
Let's jump into the demo to understand what I mean to say. Here in the input text, I have added some random numbers from 1 to 31. Regex does not directly deal with the numbers. First of all, write the regex to match the numbers between 1 to 31. In our case let's assume this is our date. If I write expressions, as you can see that this expression matches numbers 30 and 31.
Now use alternation characters and write a regex to match other numbers between 10 to 29. Add this number in character class followed by 0 to 9. This pattern matches the numbers from 10 to 29. Continue with our expression to match this further number, which is from 1 to 9. For that add an alternation character then 0(1-9).
Now it matches the remaining number from 1 to 9. But you have noticed that this number is still not matching because I have not added zero over here. If I add zero, then it starts matching. On the date there can be zero or that can be a not. Make this zero optional by adding a question mark over here. Now it started matching all the remaining numbers.
In the same way, we can write the regex to match the month between 1 to 12. Write the full regex to match the date. Before that, add some valid and invalidate in the input text over here. First, add the carrot sign over here. This portion is now completed to match the date. Add in one group followed by a forward slash and then for a month if I write zero to two, now this portion matches the month number 10, 11, and 12.
For the remaining month number, use alternation characters and then zero. Make this zero optional and the last set consisting of four digits. For that, use backslash d and end this expression. This is how we can create a regex to match the date of the format (dd/mm/yyyy).
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