How to validate ISIN using Regular Expressions

• Last Updated : 02 Mar, 2023

ISIN stands for International Securities Identification Number.

Given string str, the task is to check whether the given string is a valid ISIN(International Securities Identification Number) or not by using Regular Expression. The valid ISIN(International Securities Identification Number) must satisfy the following conditions:

1. It Should be the combination of digits and alphabets, and sometimes it includes a hyphen(-) also.
2. If ISIN Contains a hyphen (-) then Its length should be equal to 14, else length should be equal to 12.
4. It should end with digits.
5. It Should not contain white spaces.
6. Apart from Hyphen Symbol (-), It should not contain any special characters.

Examples:

Input: str=â€ťUS012071998â€ť
Output: true
Explanation: As it starts with alphabets, ends with digit and length is equal to 12.

Input: str=â€ťUS-01207199-8â€ť
Output: true
Explanation: It contains hyphen(-), Hence its length should be equal to 14.

Input: str=â€ť@US-12345â€ť
Output: false
Explanation: It starts with special symbol “@” and not satisfying with the proper format of ISIN Codes

Input: str=â€ťXS9136812895â€ť
Output: false
Explanation: Its length is greater than 12.

Input: str=â€ťIN01012023â€ť
Output: false
Explanation: Its length is not equal to 12.

Approach:

The Idea is to use Regular Expression. Regex will validate the entered data and will provide the exact format. Below are steps that can be taken for the problem:

The regex pattern to validate the ISIN code should be as written below:

regex = “^[A-Z]{2}[-]{0, 1}[0-9A-Z]{8}[-]{0, 1}[0-9]{1}\$”

Where,

• ^ Indicates starts of the string
• [A-Z]{2} matches two preceding characters in the range from “A” to “Z”.
• [-]{0, 1} will match one or zero preceding hyphen symbol  in the string.
• [0-9A-Z]{8} This will match 8 of the preceding items in the range of “A” to “Z” and 0 to 9.
• [0-9]{1} It will match one of the preceding items in the range of 0 to 9.

Follow the below steps to implement the idea:

• Create the pattern.
• Match the given string with the regular expression. In Java, this can be done by using Pattern.matcher().
• Return true if the string matches with the given regular expression, else return false.

Below is the implementation of the above approach.

C++

 `// C++ program to validate the``// ISIN Code using Regular``// Expression` `#include ``#include ``using` `namespace` `std;` `// Function to validate the``// ISIN Code``string isValid_ISIN_Code(string isin_code)``{``    ``// Regex to check valid``    ``// ISIN Code.``    ``const` `regex pattern(``"^[A-Z]{2}[-]{0,1}[0-9A-Z]{8}[-]{0,1}[0-9]{1}\$"``);` `    ``// If the isin_code``    ``// is empty return false``    ``if` `(isin_code.empty()) {``        ``return` `"false"``;``    ``}` `    ``// Return true if the isin_code``    ``// matched the ReGex``    ``if` `(regex_match(isin_code, pattern)) {``        ``return` `"true"``;``    ``}``    ``else` `{``        ``return` `"false"``;``    ``}``}` `// Driver Code``int` `main()``{``    ``// Test Case 1:``    ``string str1 = ``"US012071998"``;``    ``cout << isValid_ISIN_Code(str1) << endl;` `    ``// Test Case 2:``    ``string str2 = ``"US-01207199-8"``;``    ``cout << isValid_ISIN_Code(str2) << endl;` `    ``// Test Case 3:``    ``string str3 = ``"@US-12345"``;``    ``cout << isValid_ISIN_Code(str3) << endl;` `    ``// Test Case 4:``    ``string str4 = ``"XS9136812895"``;``    ``cout << isValid_ISIN_Code(str4) << endl;` `    ``// Test Case 5:``    ``string str5 = ``"US45256BAD38"``;``    ``cout << isValid_ISIN_Code(str5) << endl;` `    ``// Test Case 6:``    ``string str6 = ``"IN01012023"``;``    ``cout << isValid_ISIN_Code(str6) << endl;` `    ``return` `0;``}` `// This code is contributed by Aman Kumar.`

Java

 `// Java program to validate the``// ISIN Code using Regular Expression` `import` `java.util.regex.*;` `class` `GFG {``    ``// Function to validate the``    ``// ISIN Code``    ``public` `static` `boolean``    ``isValid_ISIN_Code(String isin_code)``    ``{``        ``// Regex to check valid ISIN Code``        ``String regex``            ``= ``"^[A-Z]{2}[-]{0, 1}[0-9A-Z]{8}[-]{0, 1}[0-9]{1}\$"``;` `        ``// Compile the ReGex``        ``Pattern p = Pattern.compile(regex);` `        ``// If the isin_code``        ``// is empty return false``        ``if` `(isin_code == ``null``) {``            ``return` `false``;``        ``}` `        ``// Pattern class contains matcher() method``        ``// to find matching between given``        ``// isin_code  using regular expression.``        ``Matcher m = p.matcher(isin_code);` `        ``// Return if the isin_code``        ``// matched the ReGex``        ``return` `m.matches();``    ``}` `    ``// Driver Code.``    ``public` `static` `void` `main(String args[])``    ``{``        ``// Test Case 1:``        ``String str1 = ``"US012071998"``;``        ``System.out.println(isValid_ISIN_Code(str1));` `        ``// Test Case 2:``        ``String str2 = ``"US-01207199-8"``;``        ``System.out.println(isValid_ISIN_Code(str2));` `        ``// Test Case 3:``        ``String str3 = ``"@US-12345"``;``        ``System.out.println(isValid_ISIN_Code(str3));` `        ``// Test Case 4:``        ``String str4 = ``"XS9136812895"``;``        ``System.out.println(isValid_ISIN_Code(str4));` `        ``// Test Case 5:``        ``String str5 = ``"US45256BAD38"``;``        ``System.out.println(isValid_ISIN_Code(str5));` `        ``// Test Case 6:``        ``String str6 = ``"IN01012023"``;``        ``System.out.println(isValid_ISIN_Code(str6));``    ``}``}`

Python3

 `# Python3 program to validate``# ISIN Code  using Regular Expression``import` `re` `# Function to validate ISIN``def` `isValid_ISIN_Code(``str``):` `    ``# Regex to check valid ISIN Code``    ``regex ``=` `"^[A-Z]{2}[-]{0, 1}[0-9A-Z]{8}[-]{0, 1}[0-9]{1}\$"` `    ``# 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 ``=` `"US012071998"``    ``print``(isValid_ISIN_Code(str1))``    ` `    ``# Test Case 2:``    ``str2 ``=` `"US-01207199-8"``    ``print``(isValid_ISIN_Code(str2))``    ` `    ``# Test Case 3:``    ``str3 ``=` `"@US-12345"``    ``print``(isValid_ISIN_Code(str3))``    ` `    ` `    ``# Test Case 4:``    ``str4 ``=` `"XS9136812895"``    ``print``(isValid_ISIN_Code(str4))``    ` `    ``# Test Case 5:``    ``str5 ``=` `"US45256BAD38"``    ``print``(isValid_ISIN_Code(str5))``    ` `    ``# Test Case 6:``    ``str6 ``=` `"IN01012023"``    ``print``(isValid_ISIN_Code(str6))`

C#

 `// C# program to validate the``// ISIN Code using Regular Expression` `using` `System;``using` `System.Text.RegularExpressions;` `public` `class` `GFG {``    ``// Function to validate the``    ``// ISIN Code``    ``public` `static` `bool``    ``isValid_ISIN_Code(``string` `isin_code)``    ``{``        ``// Regex to check valid ISIN Code``        ``string` `regex``            ``= ``"^[A-Z]{2}[-]{0, 1}[0-9A-Z]{8}[-]{0, 1}[0-9]{1}\$"``;` `        ``// Compile the ReGex``        ``Regex p = ``new` `Regex(regex);` `        ``// If the isin_code``        ``// is empty return false``        ``if` `(isin_code == ``null``) {``            ``return` `false``;``        ``}` `        ``// Pattern class contains matcher() method``        ``// to find matching between given``        ``// isin_code using regular expression.``        ``Match m = p.Match(isin_code);` `        ``// Return if the isin_code``        ``// matched the ReGex``        ``return` `m.Success;``    ``}` `    ``// Driver Code.``    ``public` `static` `void` `Main()``    ``{``        ``// Test Case 1:``        ``string` `str1 = ``"US012071998"``;``        ``Console.WriteLine(isValid_ISIN_Code(str1));` `        ``// Test Case 2:``        ``string` `str2 = ``"US-01207199-8"``;``        ``Console.WriteLine(isValid_ISIN_Code(str2));` `        ``// Test Case 3:``        ``string` `str3 = ``"@US-12345"``;``        ``Console.WriteLine(isValid_ISIN_Code(str3));` `        ``// Test Case 4:``        ``string` `str4 = ``"XS9136812895"``;``        ``Console.WriteLine(isValid_ISIN_Code(str4));` `        ``// Test Case 5:``        ``string` `str5 = ``"US45256BAD38"``;``        ``Console.WriteLine(isValid_ISIN_Code(str5));` `        ``// Test Case 6:``        ``string` `str6 = ``"IN01012023"``;``        ``Console.WriteLine(isValid_ISIN_Code(str6));``    ``}``}` `// This code is contributed by Pushpesh Raj.`

Javascript

 `// Javascript program to validate``// ISIN Code  using Regular Expression` `// Function to validate the``// ISIN Code ``function` `isValid_ISIN_Code(isin_code) {``    ``// Regex to check valid``    ``// ISIN CODE``    ``let regex = ``new` `RegExp(/^[A-Z]{2}[-]{0, 1}[0-9A-Z]{8}[-]{0, 1}[0-9]{1}\$/);` `    ``// ISIN CODE``    ``// is empty return false``    ``if` `(isin_code == ``null``) {``        ``return` `"false"``;``    ``}` `    ``// Return true if the isin_code``    ``// matched the ReGex``    ``if` `(regex.test(isin_code) == ``true``) {``        ``return` `"true"``;``    ``}``    ``else` `{``        ``return` `"false"``;``    ``}``}` `// Driver Code``// Test Case 1:``let str1 = ``"US012071998"``;``console.log(isValid_ISIN_Code(str1));` `// Test Case 2:``let str2 = ``"US-01207199-8"``;``console.log(isValid_ISIN_Code(str2));` `// Test Case 3:``let str3 = ``"@US-12345"``;``console.log(isValid_ISIN_Code(str3));` `// Test Case 4:``let str4 = ``"XS9136812895"``;``console.log(isValid_ISIN_Code(str4));` `// Test Case 5:``let str5 = ``"US45256BAD38"``;``console.log(isValid_ISIN_Code(str5));` `// Test Case 6:``let str6 = ``"IN01012023"``;``console.log(isValid_ISIN_Code(str6));` `// This code is contributed by Rahul Chauhan`

Output

```true
true
false
false
false
false```

Time Complexity: O(N) where N is the length of the string.
Auxiliary Space: O(1)

Related Articles:

My Personal Notes arrow_drop_up