Validating Voter ID Card using Regular Expression
Last Updated :
15 Dec, 2022
Voter ID card is also known as EPIC (Electors Photo Identity Card). EPIC is an identification proof for Indian citizens over the age of 18.
Given some Voter ids, the task is to check if they are valid or not using regular expressions.
Rules for the valid VOTER ID:
- EPIC Number is a unique alphanumeric code. combination of alphabets ( A to Z)and digits (0 to 9).
- Its length should equal to 10.
- EPIC Number starts with an alphabet and ends with a digit.
- EPIC number does not contain whitespaces and other special characters.
- EPIC number allows only Uppercase letters.
Examples:
Input: XGN3002623
Output: True
Input: XGS1234567
Output: True
Approach: The problem can be solved using a regular expression based on the following idea:
Create a regex pattern to validate the number as written below:
regex= “^[A-Z]{3}[0-9]{7}$” OR Regex=”^[A-Z]{3}\d{7}$”
Where,
- ^ : This indicates the start of the string
- [A-Z]{3} : This pattern will match 3 of the preceding items in the character range “A” to “Z”.
- [0-9]{7} : This expression will evaluate 7 of the preceding items in the range 0 to 9
- \d (Digit): It will match any digit character.
- {7} Quantifier : match 7 of the preceding item.
- $ :End of the string.
Follow the below steps to implement the idea:
- Create a regex expression for Voter ID.
- Use Pattern class to compile the regex formed.
- Use the matcher function to check whether the VOTER is valid or not.
- If it is valid, return true. Otherwise, return false.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
#include <regex>
using namespace std;
string isValidEPICNumber(string str)
{
const regex pattern( "^[A-Z]{3}[0-9]{7}$" );
if (str.empty()) {
return "false" ;
}
if (regex_match(str, pattern)) {
return "true" ;
}
else {
return "false" ;
}
}
int main()
{
string str1 = "XGN3002623" ;
cout << isValidEPICNumber(str1) << endl;
string str2 = "XGS1234567" ;
cout << isValidEPICNumber(str2) << endl;
string str3 = "BNZ1207199" ;
cout << isValidEPICNumber(str3) << endl;
string str4 = "934517865" ;
cout << isValidEPICNumber(str4) << endl;
string str5 = "12071998RAH" ;
cout << isValidEPICNumber(str5) << endl;
string str6 = "654294563" ;
cout << isValidEPICNumber(str6) << endl;
return 0;
}
|
Java
import java.util.regex.*;
import java.io.*;
class GFG {
public static boolean isValidEPICNumber(String str)
{
String regex = "^[A-Z]{3}[0-9]{7}$" ;
Pattern p = Pattern.compile(regex);
if (str == null ) {
return false ;
}
Matcher m = p.matcher(str);
return m.matches();
}
public static void main(String args[])
{
String str1 = "XGN3002623" ;
System.out.println(isValidEPICNumber(str1));
String str2 = "XGS1234567" ;
System.out.println(isValidEPICNumber(str2));
String str3 = "BNZ1207199" ;
System.out.println(isValidEPICNumber(str3));
String str4 = "934517865" ;
System.out.println(isValidEPICNumber(str4));
String str5 = "12071998RAH" ;
System.out.println(isValidEPICNumber(str5));
String str6 = "654294563" ;
System.out.println(isValidEPICNumber(str6));
}
}
|
Python3
import re
def isValidEPICNumber( str ):
regex = "^[A-Z]{3}[0-9]{7}$"
p = re. compile (regex)
if ( str = = None ):
return "false"
if (re.search(p, str )):
return "true"
else :
return "false"
if __name__ = = '__main__' :
str1 = "XGN3002623"
print (isValidEPICNumber(str1))
str2 = "XGS1234567"
print (isValidEPICNumber(str2))
str3 = "BNZ1207199"
print (isValidEPICNumber(str3))
str4 = "934517865"
print (isValidEPICNumber(str4))
str5 = "12071998RAH"
print (isValidEPICNumber(str5))
str6 = "654294563"
print (isValidEPICNumber(str6))
|
C#
using System;
using System.Text.RegularExpressions;
public class GFG {
public static bool isValidEPICNumber( string str)
{
string regex = "^[A-Z]{3}[0-9]{7}$" ;
Regex p = new Regex(regex);
if (str == null ) {
return false ;
}
Match m = p.Match(str);
return m.Success;
}
public static void Main()
{
string str1 = "XGN3002623" ;
Console.WriteLine(isValidEPICNumber(str1));
string str2 = "XGS1234567" ;
Console.WriteLine(isValidEPICNumber(str2));
string str3 = "BNZ1207199" ;
Console.WriteLine(isValidEPICNumber(str3));
string str4 = "934517865" ;
Console.WriteLine(isValidEPICNumber(str4));
string str5 = "12071998RAH" ;
Console.WriteLine(isValidEPICNumber(str5));
string str6 = "654294563" ;
Console.WriteLine(isValidEPICNumber(str6));
}
}
|
Javascript
function isValidEPICNumber(str) {
let regex = new RegExp(/^[A-Z]{3}[0-9]{7}$/);
if (str == null ) {
return "false" ;
}
if (regex.test(str) == true ) {
return "true" ;
}
else {
return "false" ;
}
}
let str1 = "XGN3002623" ;
console.log(isValidEPICNumber(str1));
let str2 = "XGS1234567" ;
console.log(isValidEPICNumber(str2));
let str3 = "BNZ1207199" ;
console.log(isValidEPICNumber(str3));
let str4 = "934517865" ;
console.log(isValidEPICNumber(str4));
let str5 = "12071998RAH" ;
console.log(isValidEPICNumber(str5));
let str6 = "654294563" ;
console.log(isValidEPICNumber(str6));
|
Output
true
true
true
false
false
false
Time Complexity: O(N) for each test case, where N is the length of the given string.
Auxiliary Space: O(1)
Related Articles:
Share your thoughts in the comments
Please Login to comment...