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
DSA Tutorial - Learn Data Structures and Algorithms DSA (Data Structures and Algorithms) is the study of organizing data efficiently using data structures like arrays, stacks, and trees, paired with step-by-step procedures (or algorithms) to solve problems effectively. Data structures manage how data is stored and accessed, while algorithms focus on
7 min read
Quick Sort QuickSort is a sorting algorithm based on the Divide and Conquer that picks an element as a pivot and partitions the given array around the picked pivot by placing the pivot in its correct position in the sorted array. It works on the principle of divide and conquer, breaking down the problem into s
12 min read
Merge Sort - Data Structure and Algorithms Tutorials Merge sort is a popular sorting algorithm known for its efficiency and stability. It follows the divide-and-conquer approach. It works by recursively dividing the input array into two halves, recursively sorting the two halves and finally merging them back together to obtain the sorted array. Merge
14 min read
Data Structures Tutorial Data structures are the fundamental building blocks of computer programming. They define how data is organized, stored, and manipulated within a program. Understanding data structures is very important for developing efficient and effective algorithms. What is Data Structure?A data structure is a st
2 min read
Bubble Sort Algorithm Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order. This algorithm is not suitable for large data sets as its average and worst-case time complexity are quite high.We sort the array using multiple passes. After the fir
8 min read
Breadth First Search or BFS for a Graph Given a undirected graph represented by an adjacency list adj, where each adj[i] represents the list of vertices connected to vertex i. Perform a Breadth First Search (BFS) traversal starting from vertex 0, visiting vertices from left to right according to the adjacency list, and return a list conta
15+ min read
Binary Search Algorithm - Iterative and Recursive Implementation Binary Search Algorithm is a searching algorithm used in a sorted array by repeatedly dividing the search interval in half. The idea of binary search is to use the information that the array is sorted and reduce the time complexity to O(log N). Binary Search AlgorithmConditions to apply Binary Searc
15 min read
Insertion Sort Algorithm Insertion sort is a simple sorting algorithm that works by iteratively inserting each element of an unsorted list into its correct position in a sorted portion of the list. It is like sorting playing cards in your hands. You split the cards into two groups: the sorted cards and the unsorted cards. T
9 min read
Array Data Structure Guide In this article, we introduce array, implementation in different popular languages, its basic operations and commonly seen problems / interview questions. An array stores items (in case of C/C++ and Java Primitive Arrays) or their references (in case of Python, JS, Java Non-Primitive) at contiguous
4 min read
Sorting Algorithms A Sorting Algorithm is used to rearrange a given array or list of elements in an order. For example, a given array [10, 20, 5, 2] becomes [2, 5, 10, 20] after sorting in increasing order and becomes [20, 10, 5, 2] after sorting in decreasing order. There exist different sorting algorithms for differ
3 min read