Link Search Menu Expand Document

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.

RegEx Use Case Realtime

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.

RegEx Use Case Explained

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:


Back to top

© , Regexsonline.com — All Rights Reserved - Terms of Use - Privacy Policy