Open in App
Not now

# 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

 `// Java program to validate the` `// PF Account Number using Regular Expression`   `import` `java.util.regex.*;`   `class` `GFG {`   `    ``// Function to validate the` `    ``// PF Account Number` `    ``public` `static` `boolean` `    ``isValid_PF_AccountNumber(String str)` `    ``{` `        ``// Regex to check valid PF Account Number` `        ``String regex` `            ``= ``"^[A-Z]{2}[\\s\\/]?[A-Z]{3}[\\s\\/]?[0-9]{7}[\\s\\/]?[0-9]{3}[\\s\\/]?[0-9]{7}\$"``;`   `        ``// Compile the ReGex` `        ``Pattern p = Pattern.compile(regex);`   `        ``// If the str is empty return false` `        ``if` `(str == ``null``) {` `            ``return` `false``;` `        ``}`   `        ``// Pattern class contains matcher()` `        ``// method to find matching between` `        ``// given str using regex.` `        ``Matcher m = p.matcher(str);`   `        ``// Return if the str` `        ``// matched the ReGex` `        ``return` `m.matches();` `    ``}`   `    ``// Driver Code.` `    ``public` `static` `void` `main(String args[])` `    ``{` `        ``// Test Case 1:` `        ``String str1 = ``"TN MAS 1207199 123 1234567"``;` `        ``System.out.println(isValid_PF_AccountNumber(str1));`   `        ``// Test Case 2:` `        ``String str2 = ``"TN/MAS/1207199/123/1234567"``;` `        ``System.out.println(isValid_PF_AccountNumber(str2));`   `        ``// Test Case 3:` `        ``String str3 = ``"TNMAS12071991231234567"``;` `        ``System.out.println(isValid_PF_AccountNumber(str3));`   `        ``// Test Case 4:` `        ``String str4 = ``"TN/MAS/1207199/123"``;` `        ``System.out.println(isValid_PF_AccountNumber(str4));`   `        ``// Test Case 5:` `        ``String str5 = ``"12071998AZUP"``;` `        ``System.out.println(isValid_PF_AccountNumber(str5));` `    ``}` `}`

## C++

 `// C++ program to validate the` `// PF Account Number using Regular` `// Expression`   `#include ` `#include ` `using` `namespace` `std;`   `// Function to validate the` `// PF Account Number` `bool` `isValid_PF_AccountNumber(string str)` `{`   `    ``// Regex to check valid` `    ``// PF Account Number` `    ``const` `regex pattern(` `        ``"^[A-Z]{2}[\\s\\/]?[A-Z]{3}[\\s\\/]?[0-9]{7}[\\s\\/"` `        ``"]?[0-9]{3}[\\s\\/]?[0-9]{7}\$"``);`   `    ``// If the str` `    ``// is empty return false` `    ``if` `(str.empty()) {` `        ``return` `false``;` `    ``}`   `    ``// Return true if the str` `    ``// matched the ReGex` `    ``if` `(regex_match(str, pattern)) {` `        ``return` `true``;` `    ``}` `    ``else` `{` `        ``return` `false``;` `    ``}` `}`   `// Driver Code` `int` `main()` `{` `    ``// Test Case 1:` `    ``string str1 = ``"TN MAS 1207199 123 1234567"``;` `    ``cout << isValid_PF_AccountNumber(str1) << endl;`   `    ``// Test Case 2:` `    ``string str2 = ``"TN/MAS/1207199/123/1234567"``;` `    ``cout << isValid_PF_AccountNumber(str2) << endl;`   `    ``// Test Case 3:` `    ``string str3 = ``"TNMAS12071991231234567"``;` `    ``cout << isValid_PF_AccountNumber(str3) << endl;`   `    ``// Test Case 4:` `    ``string str4 = ``"TN/MAS/1207199/123"``;` `    ``cout << isValid_PF_AccountNumber(str4) << endl;`   `    ``// Test Case 5:` `    ``string str5 = ``"12071998AZUP"``;` `    ``cout << isValid_PF_AccountNumber(str5) << endl;`   `    ``return` `0;` `}`

## Python

 `# Python3 program to validate` `# PF Account Number using Regular Expression` `import` `re`   `# Function to validate` `# PF Account Number`     `def` `isValid_PF_AccountNumber(``str``):`   `    ``# Regex to check valid PF Account Number` `    ``regex ``=` `"^[A-Z]{2}[\\s\\/]?[A-Z]{3}[\\s\\/]?[0-9]{7}[\\s\\/]?[0-9]{3}[\\s\\/]?[0-9]{7}\$"`   `    ``# Compile the ReGex` `    ``p ``=` `re.``compile``(regex)`   `    ``# If the string is empty` `    ``# return false` `    ``if` `(``str` `=``=` `None``):` `        ``return` `False`   `    ``# Return if the string` `    ``# matched the ReGex` `    ``if``(re.search(p, ``str``)):` `        ``return` `True` `    ``else``:` `        ``return` `False`   `# Driver code`     `# Test Case 1:` `str1 ``=` `"TN MAS 1207199 123 1234567"` `print``(isValid_PF_AccountNumber(str1))`   `# Test Case 2:` `str2 ``=` `"TN/MAS/1207199/123/1234567"` `print``(isValid_PF_AccountNumber(str2))`   `# Test Case 3:` `str3 ``=` `"TNMAS12071991231234567"` `print``(isValid_PF_AccountNumber(str3))`   `# Test Case 4:` `str4 ``=` `"TN/MAS/1207199/123"` `print``(isValid_PF_AccountNumber(str4))`   `# Test Case 5:` `str5 ``=` `"12071998AZUP"` `print``(isValid_PF_AccountNumber(str5))`

## C#

 `// C# program to validate the` `// PF Account Number` `// using Regular Expressions` `using` `System;` `using` `System.Text.RegularExpressions;` `class` `GFG {`   `    ``// Main Method` `    ``static` `void` `Main(``string``[] args)` `    ``{`   `        ``// Input strings to Match` `        ``// PF Account Number` `        ``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();` `    ``}`   `    ``// method containing the regex` `    ``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

 `// Javascript program to validate` `// PF Account Number using Regular Expression`   `// Function to validate the` `// PF Account Number` `function` `isValid_PF_AccountNumber(str) {` `    ``// Regex to check valid` `    ``// PF Account Number` `    ``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` `    ``// is empty return false` `    ``if` `(str == ``null``) {` `        ``return` `"false"``;` `    ``}`   `    ``// Return true if the str` `    ``// matched the ReGex` `    ``if` `(regex.test(str) == ``true``) {` `        ``return` `"true"``;` `    ``}` `    ``else` `{` `        ``return` `"false"``;` `    ``}` `}`   `// Driver Code` `// Test Case 1:` `let str1 = ``"TN MAS 1207199 123 1234567"``;` `console.log(isValid_PF_AccountNumber(str1));`   `// Test Case 2:` `let str2 = ``"TN/MAS/1207199/123/1234567"``;` `console.log(isValid_PF_AccountNumber(str2));`   `// Test Case 3:` `let str3 = ``"TNMAS12071991231234567"``;` `console.log(isValid_PF_AccountNumber(str3));`   `// Test Case 4:` `let str4 = ``"TN/MAS/1207199/123"``;` `console.log(isValid_PF_AccountNumber(str4));`   `// Test Case 5:` `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:

My Personal Notes arrow_drop_up
Related Articles