Regular Expressions to Validate Account Office Reference Number
Last Updated :
31 Jan, 2023
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: True
Input: 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
import java.util.regex.*;
class GFG {
public static boolean
isValid_AccOffcRefNumber(String str)
{
String regex = "^[0-9]{3}[A-Z]{2}[0-9]{8}$" ;
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 = "123IN12345678" ;
System.out.println(isValid_AccOffcRefNumber(str1));
String str2 = "123AZ12345678" ;
System.out.println(isValid_AccOffcRefNumber(str2));
String str3 = "123456AB78901" ;
System.out.println(isValid_AccOffcRefNumber(str3));
String str4 = "AA123456789AB" ;
System.out.println(isValid_AccOffcRefNumber(str4));
String str5 = "AA12345678012" ;
System.out.println(isValid_AccOffcRefNumber(str5));
}
}
|
C++
#include <iostream>
#include <regex>
using namespace std;
bool isValid_AccOffcRefNumber(string str)
{
const regex pattern( "^[0-9]{3}[A-Z]{2}[0-9]{8}$" );
if (str.empty()) {
return false ;
}
if (regex_match(str, pattern)) {
return true ;
}
else {
return false ;
}
}
int main()
{
string str1 = "123IN12345678" ;
cout << isValid_AccOffcRefNumber(str1) << endl;
string str2 = "123AZ12345678" ;
cout << isValid_AccOffcRefNumber(str2) << endl;
string str3 = "123456AB78901" ;
cout << isValid_AccOffcRefNumber(str3) << endl;
string str4 = "AA123456789AB" ;
cout << isValid_AccOffcRefNumber(str4) << endl;
string str5 = "AA12345678012" ;
cout << isValid_AccOffcRefNumber(str5) << endl;
return 0;
}
|
Python3
import re
def isValid_AccOffcRefNumber( str ):
regex = "^[0-9]{3}[A-Z]{2}[0-9]{8}$"
p = re. compile (regex)
if ( str = = None ):
return False
if (re.search(p, str )):
return True
else :
return False
str1 = "123IN12345678"
print (isValid_AccOffcRefNumber(str1))
str2 = "123AZ12345678"
print (isValid_AccOffcRefNumber(str2))
str3 = "123456AB78901"
print (isValid_AccOffcRefNumber(str3))
str4 = "AA123456789AB"
print (isValid_AccOffcRefNumber(str4))
str5 = "AA12345678012"
print (isValid_AccOffcRefNumber(str5))
|
C#
using System;
using System.Text.RegularExpressions;
class GFG {
static void Main( string [] args)
{
string [] str = { "123IN12345678" , "123AZ12345678" ,
"123456AB78901" , "AA123456789AB" ,
"AA12345678012" };
foreach ( string s in str)
{
Console.WriteLine(isValid_AccOffcRefNumber(s)
? "true"
: "false" );
}
Console.ReadKey();
}
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
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
function isValid_AccOffcRefNumber(str) {
let regex = new RegExp(/^[0-9]{3}[A-Z]{2}[0-9]{8}$/);
if (str == null ) {
return "false" ;
}
if (regex.test(str) == true ) {
return "true" ;
}
else {
return "false" ;
}
}
let str1 = "123IN12345678" ;
console.log(isValid_AccOffcRefNumber(str1));
let str2 = "123AZ12345678" ;
console.log(isValid_AccOffcRefNumber(str2));
let str3 = "123456AB78901" ;
console.log(isValid_AccOffcRefNumber(str3));
let str4 = "AA123456789AB" ;
console.log(isValid_AccOffcRefNumber(str4));
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:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...