How to validate ISIN using Regular Expressions
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:
- It Should be the combination of digits and alphabets, and sometimes it includes a hyphen(-) also.
- If ISIN Contains a hyphen (-) then Its length should be equal to 14, else length should be equal to 12.
- ISIN code must start with alphabets only.
- It should end with digits.
- It Should not contain white spaces.
- 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++
#include <bits/stdc++.h>
#include <regex>
using namespace std;
string isValid_ISIN_Code(string isin_code)
{
const regex pattern( "^[A-Z]{2}[-]{0,1}[0-9A-Z]{8}[-]{0,1}[0-9]{1}$" );
if (isin_code.empty()) {
return "false" ;
}
if (regex_match(isin_code, pattern)) {
return "true" ;
}
else {
return "false" ;
}
}
int main()
{
string str1 = "US012071998" ;
cout << isValid_ISIN_Code(str1) << endl;
string str2 = "US-01207199-8" ;
cout << isValid_ISIN_Code(str2) << endl;
string str3 = "@US-12345" ;
cout << isValid_ISIN_Code(str3) << endl;
string str4 = "XS9136812895" ;
cout << isValid_ISIN_Code(str4) << endl;
string str5 = "US45256BAD38" ;
cout << isValid_ISIN_Code(str5) << endl;
string str6 = "IN01012023" ;
cout << isValid_ISIN_Code(str6) << endl;
return 0;
}
|
Java
import java.util.regex.*;
class GFG {
public static boolean
isValid_ISIN_Code(String isin_code)
{
String regex
= "^[A-Z]{2}[-]{0, 1}[0-9A-Z]{8}[-]{0, 1}[0-9]{1}$" ;
Pattern p = Pattern.compile(regex);
if (isin_code == null ) {
return false ;
}
Matcher m = p.matcher(isin_code);
return m.matches();
}
public static void main(String args[])
{
String str1 = "US012071998" ;
System.out.println(isValid_ISIN_Code(str1));
String str2 = "US-01207199-8" ;
System.out.println(isValid_ISIN_Code(str2));
String str3 = "@US-12345" ;
System.out.println(isValid_ISIN_Code(str3));
String str4 = "XS9136812895" ;
System.out.println(isValid_ISIN_Code(str4));
String str5 = "US45256BAD38" ;
System.out.println(isValid_ISIN_Code(str5));
String str6 = "IN01012023" ;
System.out.println(isValid_ISIN_Code(str6));
}
}
|
Python3
import re
def isValid_ISIN_Code( str ):
regex = "^[A-Z]{2}[-]{0, 1}[0-9A-Z]{8}[-]{0, 1}[0-9]{1}$"
p = re. compile (regex)
if ( str = = None ):
return False
if (re.search(p, str )):
return True
else :
return False
if __name__ = = '__main__' :
str1 = "US012071998"
print (isValid_ISIN_Code(str1))
str2 = "US-01207199-8"
print (isValid_ISIN_Code(str2))
str3 = "@US-12345"
print (isValid_ISIN_Code(str3))
str4 = "XS9136812895"
print (isValid_ISIN_Code(str4))
str5 = "US45256BAD38"
print (isValid_ISIN_Code(str5))
str6 = "IN01012023"
print (isValid_ISIN_Code(str6))
|
C#
using System;
using System.Text.RegularExpressions;
public class GFG {
public static bool
isValid_ISIN_Code( string isin_code)
{
string regex
= "^[A-Z]{2}[-]{0, 1}[0-9A-Z]{8}[-]{0, 1}[0-9]{1}$" ;
Regex p = new Regex(regex);
if (isin_code == null ) {
return false ;
}
Match m = p.Match(isin_code);
return m.Success;
}
public static void Main()
{
string str1 = "US012071998" ;
Console.WriteLine(isValid_ISIN_Code(str1));
string str2 = "US-01207199-8" ;
Console.WriteLine(isValid_ISIN_Code(str2));
string str3 = "@US-12345" ;
Console.WriteLine(isValid_ISIN_Code(str3));
string str4 = "XS9136812895" ;
Console.WriteLine(isValid_ISIN_Code(str4));
string str5 = "US45256BAD38" ;
Console.WriteLine(isValid_ISIN_Code(str5));
string str6 = "IN01012023" ;
Console.WriteLine(isValid_ISIN_Code(str6));
}
}
|
Javascript
function isValid_ISIN_Code(isin_code) {
let regex = new RegExp(/^[A-Z]{2}[-]{0, 1}[0-9A-Z]{8}[-]{0, 1}[0-9]{1}$/);
if (isin_code == null ) {
return "false" ;
}
if (regex.test(isin_code) == true ) {
return "true" ;
}
else {
return "false" ;
}
}
let str1 = "US012071998" ;
console.log(isValid_ISIN_Code(str1));
let str2 = "US-01207199-8" ;
console.log(isValid_ISIN_Code(str2));
let str3 = "@US-12345" ;
console.log(isValid_ISIN_Code(str3));
let str4 = "XS9136812895" ;
console.log(isValid_ISIN_Code(str4));
let str5 = "US45256BAD38" ;
console.log(isValid_ISIN_Code(str5));
let str6 = "IN01012023" ;
console.log(isValid_ISIN_Code(str6));
|
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:
Last Updated :
02 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...