Regular Expressions to validate Loan Account Number (LAN)
Last Updated :
09 Feb, 2023
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: True
Input: 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
import java.util.regex.*;
class GFG {
public static boolean isValidLoanAccNumber(String str)
{
String regex
= "^[A-Z]{2, }[0-9]{2, }[A-Z]{1, }[0-9]{5, }$" ;
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 = "AZA22E001144075" ;
System.out.println(isValidLoanAccNumber(str1));
String str2 = "AZA@123" ;
System.out.println(isValidLoanAccNumber(str2));
String str3 = "AZA22E00 1144075" ;
System.out.println(isValidLoanAccNumber(str3));
String str4 = "@934517865" ;
System.out.println(isValidLoanAccNumber(str4));
String str5 = "AZA1234_123" ;
System.out.println(isValidLoanAccNumber(str5));
}
}
|
C#
using System;
using System.Text.RegularExpressions;
class GFG {
static void Main( string [] args)
{
string [] str = { "AZA22E001144075" , "AZA@123" ,
"AZA22E00 1144075" , "@934517865" ,
"AZA1234_123" };
foreach ( string s in str)
{
Console.WriteLine(
isValidLoanAccNumber(s) ? "true" : "false" );
}
Console.ReadKey();
}
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++
#include <iostream>
#include <regex>
using namespace std;
bool isValidLoanAccNumber(string str)
{
const regex pattern(
"^[A-Z]{2, }[0-9]{2, }[A-Z]{1, }[0-9]{5, }$" );
if (str.empty()) {
return false ;
}
if (regex_match(str, pattern)) {
return true ;
}
else {
return false ;
}
}
int main()
{
string str1 = "AZA22E001144075" ;
cout << isValidLoanAccNumber(str1) << endl;
string str2 = "AZA@123" ;
cout << isValidLoanAccNumber(str2) << endl;
string str3 = "AZA22E00 1144075" ;
cout << isValidLoanAccNumber(str3) << endl;
string str4 = "@934517865" ;
cout << isValidLoanAccNumber(str4) << endl;
string str5 = "AZA1234_123" ;
cout << isValidLoanAccNumber(str5) << endl;
return 0;
}
|
Python3
import re
def isValidLoanAccNumber( str ):
regex = "^[A-Z]{2, }[0-9]{2, }[A-Z]{1, }[0-9]{5, }$"
p = re. compile (regex)
if ( str = = None ):
return False
if (re.search(p, str )):
return True
else :
return False
str1 = "AZA22E001144075"
print (isValidLoanAccNumber(str1))
str2 = "AZA@123"
print (isValidLoanAccNumber(str2))
str3 = "AZA22E00 1144075"
print (isValidLoanAccNumber(str3))
str4 = "@934517865"
print (isValidLoanAccNumber(str4))
str5 = "AZA1234_123"
print (isValidLoanAccNumber(str5))
|
PHP
<?php
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
function isValidLoanAccNumber(str) {
let regex = new RegExp(/^[A-Z]{2, }[0-9]{2, }[A-Z]{1, }[0-9]{5, }$/);
if (str == null ) {
return "false" ;
}
if (regex.test(str) == true ) {
return "true" ;
}
else {
return "false" ;
}
}
let str1 = "AZA22E001144075" ;
console.log(isValidLoanAccNumber(str1));
let str2 = "AZA@123" ;
console.log(isValidLoanAccNumber(str2));
let str3 = "AZA22E00 1144075" ;
console.log(isValidLoanAccNumber(str3));
let str4 = "@934517865" ;
console.log(isValidLoanAccNumber(str4));
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:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...