VOOZH about

URL: https://www.geeksforgeeks.org/dsa/regular-expressions-to-validate-provident-fundpf-account-number/

⇱ Regular Expressions to Validate Provident Fund(PF) Account Number - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Regular Expressions to Validate Provident Fund(PF) Account Number

Last Updated : 23 Jul, 2025

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:


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:

Comment