Aadhar number is a 12-digit unique identity number that is being provided for the citizens of India and resident foreign nationals who have spent over 182 days in the last 1 years. It can be obtained voluntarily by people. It provides several benefits to people, unifying processes across various sectors. In this article let’s understand how we can create a regex for Aadhaar number and how regex can be matched for Aadhaar number.
Regex (short for regular expression) is a powerful tool used for searching and manipulating text. It is composed of a sequence of characters that define a search pattern. Regex can be used to find patterns in large amounts of text, validate user input, and manipulate strings. It is widely used in programming languages, text editors, and command line tools.
Let’s look at conditions that we have to satisfy to make sure that the Aadhaar number is valid-
Conditions to match Aadhaar Number
- It should have 12 digits.
- It should not start with 0 and 1.
- It should not contain any alphabet and special characters.
- It should have white space after every 4 digits.
Regex for checking if age is greater than 18 years
Regular Expression-
/^[2-9]{1}[0-9]{3}\s[0-9]{4}\s[0-9]{4}$/gm
Test string examples for the above regex-
Input String | Match Output |
---|---|
0463 2323 5754 | does not match |
3822 4546 3674 | matches |
34534 3233 111 | does not match |
1244 1222 %$11 | does not match |
Here is a detailed explanation of the above regex-
/^[2-9]{1}[0-9]{3}\s[0-9]{4}\s[0-9]{4}$/gm
^ asserts position at start of the string
Match a single character present in the list below [2-9]
{1} matches the previous token exactly one time (meaningless quantifier)
2-9 matches a single character in the range between 2 (index 50) and 9 (index 57) (case sensitive)
Match a single character present in the list below [0-9]
{3} matches the previous token exactly 3 times
0-9 matches a single character in the range between 0 (index 48) and 9 (index 57) (case sensitive)
\s matches any whitespace character (equivalent to [\r\n\t\f\v ])
Match a single character present in the list below [0-9]
{4} matches the previous token exactly 4 times
0-9 matches a single character in the range between 0 (index 48) and 9 (index 57) (case sensitive)
\s matches any whitespace character (equivalent to [\r\n\t\f\v ])
Match a single character present in the list below [0-9]
{4} matches the previous token exactly 4 times
0-9 matches a single character in the range between 0 (index 48) and 9 (index 57) (case sensitive)
$ asserts position at the end of the string, or before the line terminator right at the end of the string (if any)
Global pattern flags
g modifier: global. All matches (don't return after first match)
m modifier: multi line. Causes ^ and $ to match the begin/end of each line (not only begin/end of string)
In conclusion, Aadhar number is a crucial 12-digit identity for Indian citizens and resident foreign nationals. In this article, we explored the power of regular expressions (regex) and how they can be used to validate Aadhaar numbers. By following specific conditions and using the provided regex pattern, we can ensure the validity of Aadhaar numbers. Regex serves as a valuable tool in various programming languages and applications for pattern matching and text manipulation, making it an essential skill for developers and data analysts.