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:
- 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++
// C++ program to validate the
// ISIN Code using Regular
// Expression
#include <bits/stdc++.h>
#include <regex>
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
Outputtrue
true
false
false
false
false
Time Complexity: O(N) where N is the length of the string.
Auxiliary Space: O(1)
Related Articles:
Similar Reads
How to validate IFSC Code using Regular Expression
Given string str, the task is to check whether the given string is a valid IFSC (Indian Financial System) Code or not by using Regular Expression. The valid IFSC (Indian Financial System) Code must satisfy the following conditions: It should be 11 characters long.The first four characters should be
8 min read
How to Validate MICR Code using Regular Expression?
MICR stands for Magnetic Ink Character Recognition. This technology provides transaction security, ensuring the correctness of bank cheques. MICR code makes cheque processing faster and safer. MICR Technology reduces cheque-related fraudulent activities. Structure of a Magnetic Ink Character Recogni
5 min read
How to validate CVV number using Regular Expression
Given string str, the task is to check whether it is a valid CVV (Card Verification Value) number or not by using Regular Expression. The valid CVV (Card Verification Value) number must satisfy the following conditions: It should have 3 or 4 digits.It should have a digit between 0-9.It should not ha
5 min read
How to validate HTML tag using Regular Expression
Given string str, the task is to check whether it is a valid HTML tag or not by using Regular Expression.The valid HTML tag must satisfy the following conditions: It should start with an opening tag (<).It should be followed by a double quotes string or single quotes string.It should not allow on
6 min read
How to validate pin code of India using Regular Expression
Given a string of positive number ranging from 0 to 9, the task is to check whether the number is valid pin code or not by using a Regular Expression. The valid pin code of India must satisfy the following conditions. It can be only six digits.It should not start with zero.First digit of the pin cod
6 min read
How to validate MAC address using Regular Expression
Given string str, the task is to check whether the given string is a valid MAC address or not by using Regular Expression. A valid MAC address must satisfy the following conditions: It must contain 12 hexadecimal digits.One way to represent them is to form six pairs of the characters separated with
6 min read
How to validate a domain name using Regular Expression
Given string str, the task is to check whether the given string is a valid domain name or not by using Regular Expression.The valid domain name must satisfy the following conditions: The domain name should be a-z or A-Z or 0-9 and hyphen (-).The domain name should be between 1 and 63 characters long
6 min read
How to validate a Username using Regular Expressions in Java
Given a string str which represents a username, the task is to validate this username with the help of Regular Expressions. A username is considered valid if all the following constraints are satisfied: The username consists of 6 to 30 characters inclusive. If the username consists of less than 6 or
3 min read
How to validate MasterCard number using Regular Expression
Given string str, the task is to check whether the given string is a valid Master Card number or not by using Regular Expression. The valid Master Card number must satisfy the following conditions. It should be 16 digits long.It should start with either two digits numbers may range from 51 to 55 or
7 min read
Validate Gender using Regular Expressions
Given some words of Gender, the task is to check if they are valid or not using regular expressions. The correct responses can be as given below: Male / male / MALE / M / mFemale / female / FEMALE / F / fNot prefer to say Example: Input: MOutput: True Input: SOutput: False Approach: The problem can
6 min read