# Validate LEI(Legal Identity Identifier) using Regular Expression

• Last Updated : 20 Dec, 2022

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 consecutely.

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++

```// C++ program to validate the
// LEI(Legal Identity Identifier) Code
// using Regular Expression
#include <bits/stdc++.h>
#include <regex>
using namespace std;

// Function to validate the
// LEI Code
string isValid_LEICode(string str)
{
// Regex to check valid
// LEI Code.
const regex pattern(
"^[0-9]{4}[0]{2}[A-Z0-9]{12}[0-9]{2}\$");

// 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 = "199800RAH1207XYDCQ23";
cout << isValid_LEICode(str1) << endl;

// Test Case 2:
string str2 = "123400ABCDE7810FGH07";
cout << isValid_LEICode(str2) << endl;

// Test Case 3:
string str3 = "123456ABCDE7810FGH19";
cout << isValid_LEICode(str3) << endl;

// Test Case 4:
string str4 = "123400ABCDE7810FGHMN";
cout << isValid_LEICode(str4) << endl;

// Test Case 5:
string str5 = "123400ABCDE7810F";
cout << isValid_LEICode(str5) << endl;

// Test Case 6:
string str6 = "AKAN00ABCDE7810FGH12";
cout << isValid_LEICode(str6) << endl;

return 0;
}```

## Java

```// Java program to validate the
// LEI(Legal Identity Identifier) code
// using Regular Expression
import java.util.regex.*;

class GFG {

// Function to validate the
// LEI Code
public static boolean isValid_LEICode(String str)
{

// Regex to check valid LEI Code
String regex
= "^[0-9]{4}[0]{2}[A-Z0-9]{12}[0-9]{2}\$";

// Compile the Regex
Pattern p = Pattern.compile(regex);

// If the LEI Code
// is empty return false
if (str == null) {
return false;
}

// Pattern class contains matcher()
// method to find matching between
// given LEI Code using regex.
Matcher m = p.matcher(str);

// Return if the MICR Code
// matched the ReGex
return m.matches();
}

// Driver Code.
public static void main(String args[])
{

// Test Case 1:
String str1 = "199800RAH1207XYDCQ23";
System.out.println(isValid_LEICode(str1));

// Test Case 2:
String str2 = "123400ABCDE7810FGH07";
System.out.println(isValid_LEICode(str2));

// Test Case 3:
String str3 = "123456ABCDE7810FGH19";
System.out.println(isValid_LEICode(str3));

// Test Case 4:
String str4 = "123400ABCDE7810FGHMN";
System.out.println(isValid_LEICode(str4));

// Test Case 5:
String str5 = "123400ABCDE7810F";
System.out.println(isValid_LEICode(str5));

// Test Case 6:
String str6 = "AKAN00ABCDE7810FGH12";
System.out.println(isValid_LEICode(str6));
}
}```

## Python3

```# Python3 program to validate
# LEI Code using Regular Expression

import re

# Function to validate
# LEI(Legal Identity Identifier) Code
def isValid_LEICode(str):

# Regex to check valid LEI Code
regex = "^[0-9]{4}[0]{2}[A-Z0-9]{12}[0-9]{2}\$"

# 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
if __name__ == '__main__':

# Test Case 1:
str1 = "199800RAH1207XYDCQ23"
print(isValid_LEICode(str1))

# Test Case 2:
str2 = "123400ABCDE7810FGH07"
print(isValid_LEICode(str2))

# Test Case 3:
str3 = "123456ABCDE7810FGH19"
print(isValid_LEICode(str3))

# Test Case 4:
str4 = "123400ABCDE7810FGHMN"
print(isValid_LEICode(str4))

# Test Case 5:
str5 = "123400ABCDE7810F"
print(isValid_LEICode(str5))

# Test Case 6:
str6 = "AKAN00ABCDE7810FGH12"
print(isValid_LEICode(str6))
```

## C#

```// Include namespace system
using System;
using System.Text.RegularExpressions;

public class GFG
{

// Function to validate the
// LEI Code
public static bool isValid_LEICode(String str)
{

// Regex to check valid LEI Code
var regex = new Regex("^[0-9]{4}[0]{2}[A-Z0-9]{12}[0-9]{2}\$");

// If the LEI Code
// is empty return false
if (str == null)
{
return false;
}

// Pattern class contains matcher()
// method to find matching between
// given LEI Code using regex.
var m = regex.Match(str);

// Return if the MICR Code
// matched the ReGex
return m.Success;
}

// Driver Code.
public static void Main(String[] args)
{

// Test Case 1:
var str1 = "199800RAH1207XYDCQ23";
Console.WriteLine(GFG.isValid_LEICode(str1));

// Test Case 2:
var str2 = "123400ABCDE7810FGH07";
Console.WriteLine(GFG.isValid_LEICode(str2));

// Test Case 3:
var str3 = "123456ABCDE7810FGH19";
Console.WriteLine(GFG.isValid_LEICode(str3));

// Test Case 4:
var str4 = "123400ABCDE7810FGHMN";
Console.WriteLine(GFG.isValid_LEICode(str4));

// Test Case 5:
var str5 = "123400ABCDE7810F";
Console.WriteLine(GFG.isValid_LEICode(str5));

// Test Case 6:
var str6 = "AKAN00ABCDE7810FGH12";
Console.WriteLine(GFG.isValid_LEICode(str6));
}
}

// This code is contributed by aaityaburujwale.```

## Javascript

```// Javascript program to validate
// LEI Code using Regular Expression

// Function to validate the
// LEI(Legal Identity Identifier) Code
function isValid_LEICode(str) {
// Regex to check valid
// LEI CODE
let regex = new RegExp(/^[0-9]{4}[0]{2}[A-Z0-9]{12}[0-9]{2}\$/);

// 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 = "199800RAH1207XYDCQ23";
console.log(isValid_LEICode(str1));

// Test Case 2:
let str2 = "123400ABCDE7810FGH07";
console.log(isValid_LEICode(str2));

// Test Case 3:
let str3 = "123456ABCDE7810FGH19";
console.log(isValid_LEICode(str3));

// Test Case 4:
let str4 = "123400ABCDE7810FGHMN";
console.log(isValid_LEICode(str4));

// Test Case 5:
let str5 = "123400ABCDE7810F";
console.log(isValid_LEICode(str5));

// Test Case 6:
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:

My Personal Notes arrow_drop_up
Related Articles