Regular Expressions to Validate Account Office Reference Number
Given some Account Office Reference Number, the task is to check if they are valid or not using regular expressions. Rules for the valid Account Office Reference Number are:
- It is an alphanumeric string containing upper-case letters and digits.
- Accounts Office Reference Number is a unique, 13-character code.
- It starts with digits (0-9) and ends with digits.
- Its first three places are reserved for digits.
- After the first three characters, two places are occupied by uppercase alphabet letters (A-Z).
- After the first five characters, the next 8 places are reserved for digits (0-9).
Examples:
Input: str = ”123IN12345678.″
Output: TrueInput: str = ”IN12345678901″
Output: False
Explanation: As it starts with an alphabet letter.
Approach: The problem can be solved based on the following idea:
Create a regex pattern to validate the number as written below:
regex = “^[0-9]{3}[A-Z]{2}[0-9]{8}$“Where,
- ^ : Start of the string
- [0-9]{3}: This pattern will match 3 of the preceding items if they are digits(0-9)
- [A-Z]{2}: This pattern will match two of the preceding items if they are Uppercase Alphabet letters
- [0-9]{8}: This pattern will allow 8 of the preceding tokens if they are digits
- $: End of the string
Follow the below steps to implement the idea:
- Create a regex expression for Account Office Reference Number
- Use Pattern class to compile the regex formed.
- Use the matcher function to check whether the Account Office Reference Number is valid or not.
- If it is valid, return true. Otherwise, return false.
Below is the code implementation of the above-discussed approach:
Java
// Java program to validate the // Account Office Reference Number // using Regular Expression import java.util.regex.*; class GFG { // Function to validate the // Account Office Reference Number public static boolean isValid_AccOffcRefNumber(String str) { // Regex to check valid // Account Office Reference Number String regex = "^[0-9]{3}[A-Z]{2}[0-9]{8}$" ; // 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 regular expression 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 = "123IN12345678" ; System.out.println(isValid_AccOffcRefNumber(str1)); // Test Case 2: String str2 = "123AZ12345678" ; System.out.println(isValid_AccOffcRefNumber(str2)); // Test Case 3: String str3 = "123456AB78901" ; System.out.println(isValid_AccOffcRefNumber(str3)); // Test Case 4: String str4 = "AA123456789AB" ; System.out.println(isValid_AccOffcRefNumber(str4)); // Test Case 5: String str5 = "AA12345678012" ; System.out.println(isValid_AccOffcRefNumber(str5)); } } |
C++
// C++ program to validate the // Account Office Reference Numbers // using Regular Expression #include <iostream> #include <regex> using namespace std; // Function to validate the // Account Office Reference Number bool isValid_AccOffcRefNumber(string str) { // Regex to check valid // Account Office Reference Number const regex pattern( "^[0-9]{3}[A-Z]{2}[0-9]{8}$" ); // 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 = "123IN12345678" ; cout << isValid_AccOffcRefNumber(str1) << endl; // Test Case 2: string str2 = "123AZ12345678" ; cout << isValid_AccOffcRefNumber(str2) << endl; // Test Case 3: string str3 = "123456AB78901" ; cout << isValid_AccOffcRefNumber(str3) << endl; // Test Case 4: string str4 = "AA123456789AB" ; cout << isValid_AccOffcRefNumber(str4) << endl; // Test Case 5: string str5 = "AA12345678012" ; cout << isValid_AccOffcRefNumber(str5) << endl; return 0; } |
Python3
# Python3 program to validate # Account Office Reference Number using # Regular Expression import re # Function to validate # Account Office Reference Number def isValid_AccOffcRefNumber( str ): # Regex to check valid # Account Office Reference Number regex = "^[0-9]{3}[A-Z]{2}[0-9]{8}$" # 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 = "123IN12345678" print (isValid_AccOffcRefNumber(str1)) # Test Case 2: str2 = "123AZ12345678" print (isValid_AccOffcRefNumber(str2)) # Test Case 3: str3 = "123456AB78901" print (isValid_AccOffcRefNumber(str3)) # Test Case 4: str4 = "AA123456789AB" print (isValid_AccOffcRefNumber(str4)) # Test Case 5: str5 = "AA12345678012" print (isValid_AccOffcRefNumber(str5)) |
C#
// C# program to validate the // Account Office Reference Number // using Regular Expressions using System; using System.Text.RegularExpressions; class GFG { // Main Method static void Main( string [] args) { // Input strings to Match // Account Office Reference Number string [] str = { "123IN12345678" , "123AZ12345678" , "123456AB78901" , "AA123456789AB" , "AA12345678012" }; foreach ( string s in str) { Console.WriteLine(isValid_AccOffcRefNumber(s) ? "true" : "false" ); } Console.ReadKey(); } // method containing the regex public static bool isValid_AccOffcRefNumber( string str) { string strRegex = @"^[0-9]{3}[A-Z]{2}[0-9]{8}$" ; Regex re = new Regex(strRegex); if (re.IsMatch(str)) return ( true ); else return ( false ); } } |
PHP
<?php // PHP program to validate Account Office Reference Number // Function to validate Account Office Reference Number using regular expression function isValid_AccOffcRefNumber( $str ){ if (preg_match( '/^[0-9]{3}[A-Z]{2}[0-9]{8}$/' , $str )) { echo "true\n" ; } else { echo "false\n" ; } } isValid_AccOffcRefNumber( "123IN12345678" ); isValid_AccOffcRefNumber( "123AZ12345678" ); isValid_AccOffcRefNumber( "123456AB78901" ); isValid_AccOffcRefNumber( "AA123456789AB" ); isValid_AccOffcRefNumber( "AA12345678012" ); ?> |
Javascript
// Javascript program to validate // Account Office Reference Number //using Regular Expression // Function to validate the // Account Office Reference Number function isValid_AccOffcRefNumber(str) { // Regex to check valid // Account Office Reference Number let regex = new RegExp(/^[0-9]{3}[A-Z]{2}[0-9]{8}$/); // 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 = "123IN12345678" ; console.log(isValid_AccOffcRefNumber(str1)); // Test Case 2: let str2 = "123AZ12345678" ; console.log(isValid_AccOffcRefNumber(str2)); // Test Case 3: let str3 = "123456AB78901" ; console.log(isValid_AccOffcRefNumber(str3)); // Test Case 4: let str4 = "AA123456789AB" ; console.log(isValid_AccOffcRefNumber(str4)); // Test Case 5: let str5 = "AA12345678012" ; console.log(isValid_AccOffcRefNumber(str5)); |
Output
true true false false false
Time complexity: O(n) // where n is the length of the string.
Auxiliary space: O(1)
Related Articles:
Please Login to comment...