Regular Expressions to validate Loan Account Number (LAN)
Given some Loan Account Number(LAN), the task is to check if they are valid or not using regular expressions. Rules for the valid Loan Account Number are:
- LAN is an alphanumeric string i.e., contains only digits (0-9) and uppercase alphabet characters.
- It does not allow whitespaces in it.
- It does not contain special characters.
- It starts with Uppercase alphabets and ends with digits.
Examples:
Input: str = ”AZA22E001144075″
Output: TrueInput: str = ”Abc9136812895″
Output: False
Explanation: As it contains lowercase alphabets characters such as b and c.
Approach: The problem can be solved based on the following idea:
Create a regex pattern to validate the Loan Account Number as written below:
regex=”^[A-Z]{2, }[0-9]{2, }[A-Z]{1, }[0-9]{5, }$”
- ^ : Beginning of the string.
- [A – Z]{2, }: This pattern will match two or more than two preceding items if they are in the range A to Z.
- [0 – 9]{2, }: This pattern will match two or more than two preceding items if they are in the range 0 to 9..
- $: End of the string
Follow the below steps to implement the idea:
- Create a regex expression for Loan Account Number.
- Use Pattern class to compile the regex formed.
- Use the matcher function to check whether the Loan Account 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 // LOAN ACCOUNT NUMBER using // Regular Expression import java.util.regex.*; class GFG { // Function to validate the // LOAN ACCOUNT NUMBER // (For India Country Only) public static boolean isValidLoanAccNumber(String str) { // Regex to check valid // LOAN ACCOUNT NUMBER String regex = "^[A-Z]{2, }[0-9]{2, }[A-Z]{1, }[0-9]{5, }$" ; // 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 = "AZA22E001144075" ; System.out.println(isValidLoanAccNumber(str1)); // Test Case 2: String str2 = "AZA@123" ; System.out.println(isValidLoanAccNumber(str2)); // Test Case 3: String str3 = "AZA22E00 1144075" ; System.out.println(isValidLoanAccNumber(str3)); // Test Case 4: String str4 = "@934517865" ; System.out.println(isValidLoanAccNumber(str4)); // Test Case 5: String str5 = "AZA1234_123" ; System.out.println(isValidLoanAccNumber(str5)); } } |
C#
// C# program to validate the // LOAN ACCOUNT NUMBER // using Regular Expressions using System; using System.Text.RegularExpressions; class GFG { // Main Method static void Main( string [] args) { // Input strings to Match // LOAN ACCOUNT NUMBER string [] str = { "AZA22E001144075" , "AZA@123" , "AZA22E00 1144075" , "@934517865" , "AZA1234_123" }; foreach ( string s in str) { Console.WriteLine( isValidLoanAccNumber(s) ? "true" : "false" ); } Console.ReadKey(); } // method containing the regex public static bool isValidLoanAccNumber( string str) { string strRegex = @"^[A-Z]{2, }[0-9]{2, }[A-Z]{1, }[0-9]{5, }$" ; Regex re = new Regex(strRegex); if (re.IsMatch(str)) return ( true ); else return ( false ); } } |
C++
// C++ program to validate the // LOAN ACCOUNT NUMBER using Regular // Expression #include <iostream> #include <regex> using namespace std; // Function to validate the // LOAN ACCOUNT NUMBER bool isValidLoanAccNumber(string str) { // Regex to check valid // LOAN ACCOUNT NUMBER const regex pattern( "^[A-Z]{2, }[0-9]{2, }[A-Z]{1, }[0-9]{5, }$" ); // If the // 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 = "AZA22E001144075" ; cout << isValidLoanAccNumber(str1) << endl; // Test Case 2: string str2 = "AZA@123" ; cout << isValidLoanAccNumber(str2) << endl; // Test Case 3: string str3 = "AZA22E00 1144075" ; cout << isValidLoanAccNumber(str3) << endl; // Test Case 4: string str4 = "@934517865" ; cout << isValidLoanAccNumber(str4) << endl; // Test Case 5: string str5 = "AZA1234_123" ; cout << isValidLoanAccNumber(str5) << endl; return 0; } |
Python3
# Python3 program to validate # LOAN ACCOUNT NUMBER using Regular Expression import re # Function to validate # LOAN ACCOUNT NUMBER def isValidLoanAccNumber( str ): # Regex to check valid LOAN ACCOUNT NUMBER regex = "^[A-Z]{2, }[0-9]{2, }[A-Z]{1, }[0-9]{5, }$" # 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 = "AZA22E001144075" print (isValidLoanAccNumber(str1)) # Test Case 2: str2 = "AZA@123" print (isValidLoanAccNumber(str2)) # Test Case 3: str3 = "AZA22E00 1144075" print (isValidLoanAccNumber(str3)) # Test Case 4: str4 = "@934517865" print (isValidLoanAccNumber(str4)) # Test Case 5: str5 = "AZA1234_123" print (isValidLoanAccNumber(str5)) |
PHP
<?php // PHP program to validate LOAN ACCOUNT NUMBER // Function to validate LOAN ACCOUNT NUMBER using regular expression function isValidLoanAccNumber( $str ){ if (preg_match( '/^[A-Z]{2, }[0-9]{2, }[A-Z]{1, }[0-9]{5, }$/' , $str )) { echo "true\n" ; } else { echo "false\n" ; } } isValidLoanAccNumber( "AZA22E001144075" ); isValidLoanAccNumber( "AZA@123" ); isValidLoanAccNumber( "AZA22E00 1144075" ); isValidLoanAccNumber( "@934517865" ); isValidLoanAccNumber( "AZA1234_123" ); ?> |
Javascript
// Javascript program to validate // LOAN ACCOUNT NUMBER using Regular Expression // Function to validate the // LOAN ACCOUNT NUMBER function isValidLoanAccNumber(str) { // Regex to check valid // LOAN ACCOUNT NUMBER let regex = new RegExp(/^[A-Z]{2, }[0-9]{2, }[A-Z]{1, }[0-9]{5, }$/); // 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 = "AZA22E001144075" ; console.log(isValidLoanAccNumber(str1)); // Test Case 2: let str2 = "AZA@123" ; console.log(isValidLoanAccNumber(str2)); // Test Case 3: let str3 = "AZA22E00 1144075" ; console.log(isValidLoanAccNumber(str3)); // Test Case 4: let str4 = "@934517865" ; console.log(isValidLoanAccNumber(str4)); // Test Case 5: let str5 = "AZA1234_123" ; console.log(isValidLoanAccNumber(str5)); |
Output
true false false false false
Time complexity: O(n). // where n is length of the string.
Auxiliary Space: O(1).
Related Articles:
Please Login to comment...