Validate LEI(Legal Identity Identifier) using Regular Expression
Last Updated :
10 Feb, 2023
Given some Legal Identity Identifier, the task is to check if they are valid or not using regular expressions. Rules for the valid LEI are:
- LEI code length is comprised of a 20-digit alphanumeric code.
- The first four characters are digits.
- Next two places are reserved for digit Zero.
- Next 12 characters are reserved for an alphanumeric code containing digits and uppercase letters.
- Last two places are reserved for digits(0-9).
- It should not contain any other special characters.
Examples:
Input: str = ”123456ABCDE7810FGH19”
Output: False
Explanation: As we have pointed above that characters at places 5 and 6 is reserved for digit ZERO.
Here instead of ZERO it is occupied by 5 and 6 digit consecutively.
Input: str= ”123400ABCDE7810FGHMN”
Output: False
Explanation : Last two digits should be digits only.
Input: str=”123400ABCDE7810F”
Output: False
Explanation : LEI Code length should be equal to 20 following all the necessary conditions.
Input: str=”123400ABCDE7810FGH07”
Output: True
Explanation : This pattern satisfies all the above noted points.
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]{4}[0]{2}[A-Z0-9]{12}[0-9]{2}$“
Where,
^ : Start of the string
[0-9]{4} : This pattern will match four of the preceding items in the range from 0 to 9.
[0]{2} :This pattern will match two of the preceding items if they are ZERO.
[A-Z0-9]{12} : This pattern will match 12 of the preceding items in the range from 0 to 9 or “A” to “Z”.
[0-9]{2} : This pattern will match two of the preceding items if they are in the range from 0 to 9.
$ : Indicates the end of the string.
Follow the steps mentioned below to implement the idea:
- Create regex expression for Time formats.
- Use Pattern class to compile the regex formed.
- Use the matcher function to check whether the Time format is valid or not.
- If it is valid, return true. Otherwise, return false.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
#include <regex>
using namespace std;
string isValid_LEICode(string str)
{
const regex pattern(
"^[0-9]{4}[0]{2}[A-Z0-9]{12}[0-9]{2}$" );
if (str.empty()) {
return "false" ;
}
if (regex_match(str, pattern)) {
return "true" ;
}
else {
return "false" ;
}
}
int main()
{
string str1 = "199800RAH1207XYDCQ23" ;
cout << isValid_LEICode(str1) << endl;
string str2 = "123400ABCDE7810FGH07" ;
cout << isValid_LEICode(str2) << endl;
string str3 = "123456ABCDE7810FGH19" ;
cout << isValid_LEICode(str3) << endl;
string str4 = "123400ABCDE7810FGHMN" ;
cout << isValid_LEICode(str4) << endl;
string str5 = "123400ABCDE7810F" ;
cout << isValid_LEICode(str5) << endl;
string str6 = "AKAN00ABCDE7810FGH12" ;
cout << isValid_LEICode(str6) << endl;
return 0;
}
|
Java
import java.util.regex.*;
class GFG {
public static boolean isValid_LEICode(String str)
{
String regex
= "^[0-9]{4}[0]{2}[A-Z0-9]{12}[0-9]{2}$" ;
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 = "199800RAH1207XYDCQ23" ;
System.out.println(isValid_LEICode(str1));
String str2 = "123400ABCDE7810FGH07" ;
System.out.println(isValid_LEICode(str2));
String str3 = "123456ABCDE7810FGH19" ;
System.out.println(isValid_LEICode(str3));
String str4 = "123400ABCDE7810FGHMN" ;
System.out.println(isValid_LEICode(str4));
String str5 = "123400ABCDE7810F" ;
System.out.println(isValid_LEICode(str5));
String str6 = "AKAN00ABCDE7810FGH12" ;
System.out.println(isValid_LEICode(str6));
}
}
|
Python3
import re
def isValid_LEICode( str ):
regex = "^[0-9]{4}[0]{2}[A-Z0-9]{12}[0-9]{2}$"
p = re. compile (regex)
if ( str = = None ):
return "false"
if (re.search(p, str )):
return "true"
else :
return "false"
if __name__ = = '__main__' :
str1 = "199800RAH1207XYDCQ23"
print (isValid_LEICode(str1))
str2 = "123400ABCDE7810FGH07"
print (isValid_LEICode(str2))
str3 = "123456ABCDE7810FGH19"
print (isValid_LEICode(str3))
str4 = "123400ABCDE7810FGHMN"
print (isValid_LEICode(str4))
str5 = "123400ABCDE7810F"
print (isValid_LEICode(str5))
str6 = "AKAN00ABCDE7810FGH12"
print (isValid_LEICode(str6))
|
C#
using System;
using System.Text.RegularExpressions;
public class GFG
{
public static bool isValid_LEICode(String str)
{
var regex = new Regex( "^[0-9]{4}[0]{2}[A-Z0-9]{12}[0-9]{2}$" );
if (str == null )
{
return false ;
}
var m = regex.Match(str);
return m.Success;
}
public static void Main(String[] args)
{
var str1 = "199800RAH1207XYDCQ23" ;
Console.WriteLine(GFG.isValid_LEICode(str1));
var str2 = "123400ABCDE7810FGH07" ;
Console.WriteLine(GFG.isValid_LEICode(str2));
var str3 = "123456ABCDE7810FGH19" ;
Console.WriteLine(GFG.isValid_LEICode(str3));
var str4 = "123400ABCDE7810FGHMN" ;
Console.WriteLine(GFG.isValid_LEICode(str4));
var str5 = "123400ABCDE7810F" ;
Console.WriteLine(GFG.isValid_LEICode(str5));
var str6 = "AKAN00ABCDE7810FGH12" ;
Console.WriteLine(GFG.isValid_LEICode(str6));
}
}
|
Javascript
function isValid_LEICode(str) {
let regex = new RegExp(/^[0-9]{4}[0]{2}[A-Z0-9]{12}[0-9]{2}$/);
if (str == null ) {
return "false" ;
}
if (regex.test(str) == true ) {
return "true" ;
}
else {
return "false" ;
}
}
let str1 = "199800RAH1207XYDCQ23" ;
console.log(isValid_LEICode(str1));
let str2 = "123400ABCDE7810FGH07" ;
console.log(isValid_LEICode(str2));
let str3 = "123456ABCDE7810FGH19" ;
console.log(isValid_LEICode(str3));
let str4 = "123400ABCDE7810FGHMN" ;
console.log(isValid_LEICode(str4));
let str5 = "123400ABCDE7810F" ;
console.log(isValid_LEICode(str5));
let str6 = "AKAN00ABCDE7810FGH12" ;
console.log(isValid_LEICode(str6));
|
Output
true
true
false
false
false
false
Time Complexity: O(N) for each test case, where N is the length of the given string.
Auxiliary Space: O(1)
Related Articles:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...