Regular Expressions to Validate Provident Fund(PF) Account Number
Last Updated :
30 Jan, 2023
Given some PF(Provident Fund) Account Number, the task is to check if they are valid or not using regular expressions. Rules for the valid PF Account Number are :
- PF account number is alphanumeric String and forward slaces.
- First five characters are reserved for alphabet letters.
- Next 17 characters are reserved for digits(0-9).
- It allows only special characters whitespaces and forward slaces.
- Its length should be less than equal to if:
- It contains forward slaces: 26
- It contains whitespaces: 26.
- It does not contain forward slaces and whitespaces: 22
Examples:
Input: str = “TN MAS 1207199 123 1234567”
Output: True
Input: str = “TN/MAS/1207199/123”
Output: False
Approach: The problem can be solved based on the following idea:
Create a regex pattern to validate the number as written below:
regex = “^[A-Z]{2}[\s\/]?[A-Z]{3}[\s\/]?[0-9]{7}[\s\/]?[0-9]{3}[\s\/]?[0-9]{7}$“
Where,
- ^: Start of the string
- [A-Z]{2}: 2 alphabets characters should be there.
- [\s\/] : Either space or forward slace
- [0-9]{3} : 3 digits should be there.
Follow the below steps to implement the idea:
- Create a regex expression forPF Account Number.
- Use Pattern class to compile the regex formed.
- Use the matcher function to check whether the Account Number is valid or not.
- If it is valid, return true. Otherwise, return false.
Below is the implementation of the above approach:
Java
import java.util.regex.*;
class GFG {
public static boolean
isValid_PF_AccountNumber(String str)
{
String regex
= "^[A-Z]{2}[\\s\\/]?[A-Z]{3}[\\s\\/]?[0-9]{7}[\\s\\/]?[0-9]{3}[\\s\\/]?[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 = "TN MAS 1207199 123 1234567" ;
System.out.println(isValid_PF_AccountNumber(str1));
String str2 = "TN/MAS/1207199/123/1234567" ;
System.out.println(isValid_PF_AccountNumber(str2));
String str3 = "TNMAS12071991231234567" ;
System.out.println(isValid_PF_AccountNumber(str3));
String str4 = "TN/MAS/1207199/123" ;
System.out.println(isValid_PF_AccountNumber(str4));
String str5 = "12071998AZUP" ;
System.out.println(isValid_PF_AccountNumber(str5));
}
}
|
C++
#include <iostream>
#include <regex>
using namespace std;
bool isValid_PF_AccountNumber(string str)
{
const regex pattern(
"^[A-Z]{2}[\\s\\/]?[A-Z]{3}[\\s\\/]?[0-9]{7}[\\s\\/"
"]?[0-9]{3}[\\s\\/]?[0-9]{7}$" );
if (str.empty()) {
return false ;
}
if (regex_match(str, pattern)) {
return true ;
}
else {
return false ;
}
}
int main()
{
string str1 = "TN MAS 1207199 123 1234567" ;
cout << isValid_PF_AccountNumber(str1) << endl;
string str2 = "TN/MAS/1207199/123/1234567" ;
cout << isValid_PF_AccountNumber(str2) << endl;
string str3 = "TNMAS12071991231234567" ;
cout << isValid_PF_AccountNumber(str3) << endl;
string str4 = "TN/MAS/1207199/123" ;
cout << isValid_PF_AccountNumber(str4) << endl;
string str5 = "12071998AZUP" ;
cout << isValid_PF_AccountNumber(str5) << endl;
return 0;
}
|
Python
import re
def isValid_PF_AccountNumber( str ):
regex = "^[A-Z]{2}[\\s\\/]?[A-Z]{3}[\\s\\/]?[0-9]{7}[\\s\\/]?[0-9]{3}[\\s\\/]?[0-9]{7}$"
p = re. compile (regex)
if ( str = = None ):
return False
if (re.search(p, str )):
return True
else :
return False
str1 = "TN MAS 1207199 123 1234567"
print (isValid_PF_AccountNumber(str1))
str2 = "TN/MAS/1207199/123/1234567"
print (isValid_PF_AccountNumber(str2))
str3 = "TNMAS12071991231234567"
print (isValid_PF_AccountNumber(str3))
str4 = "TN/MAS/1207199/123"
print (isValid_PF_AccountNumber(str4))
str5 = "12071998AZUP"
print (isValid_PF_AccountNumber(str5))
|
C#
using System;
using System.Text.RegularExpressions;
class GFG {
static void Main( string [] args)
{
string [] str
= { "TN MAS 1207199 123 1234567" ,
"TN/MAS/1207199/123/1234567" ,
"TNMAS12071991231234567" ,
"TN/MAS/1207199/123" , "12071998AZUP" };
foreach ( string s in str)
{
Console.WriteLine(isValid_PF_AccountNumber(s)
? "true"
: "false" );
}
Console.ReadKey();
}
public static bool isValid_PF_AccountNumber( string str)
{
string strRegex
= @"^[A-Z]{2}[\s\/]?[A-Z]{3}[\s\/]?[0-9]{7}[\s\/]?[0-9]{3}[\s\/]?[0-9]{7}$" ;
Regex re = new Regex(strRegex);
if (re.IsMatch(str))
return ( true );
else
return ( false );
}
}
|
Javascript
function isValid_PF_AccountNumber(str) {
let regex = new RegExp(/^[A-Z]{2}[\s\/]?[A-Z]{3}[\s\/]?[0-9]{7}[\s\/]?[0-9]{3}[\s\/]?[0-9]{7}$/);
if (str == null ) {
return "false" ;
}
if (regex.test(str) == true ) {
return "true" ;
}
else {
return "false" ;
}
}
let str1 = "TN MAS 1207199 123 1234567" ;
console.log(isValid_PF_AccountNumber(str1));
let str2 = "TN/MAS/1207199/123/1234567" ;
console.log(isValid_PF_AccountNumber(str2));
let str3 = "TNMAS12071991231234567" ;
console.log(isValid_PF_AccountNumber(str3));
let str4 = "TN/MAS/1207199/123" ;
console.log(isValid_PF_AccountNumber(str4));
let str5 = "12071998AZUP" ;
console.log(isValid_PF_AccountNumber(str5));
|
Output
true
true
true
false
false
Time Complexity: O(N) for each testcase, where N is the length of the given string.
Auxiliary Space: O(1)
Related Articles:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...