How to validate ISIN using Regular Expressions?


In this problem, we will use the regular expression to validate the ISIN number in C++.

The ISIN stands for the International Securities Identification Number. It is a unique code to identify stocks, financial bonds, etc. The length of the ISIN number can be 12 or 14, which provides international recognization of the particular stuff.

Let’s understand the format of the ISIN number.

  • Country code − It starts with two characters of the country code.

  • Identifier − It contains 9 alphanumeric characters after the country code.

  • Checksum − It contains the single digit which is used to detect errors in the ISIN number.

  • It may contain the hyphen(-) after the country code and identifier.

Problem statement − We have given an ISIN number in the string format. We need to validate the ISIN number using the regular expression.

Sample Examples

Input 

str1 = "SB0123456A98"

Output 

Yes

Explanation

The ISIN number is valid as it follows all rules of the ISIN code.

Input 

str1 = "US-01207199D-8"

Output 

Yes

Explanation

It is a valid ISIN number.

Input 

str1 = "USerw01207199D8"

Output 

No

Explanation

The length of the identifier is 12. So, it is an invalid ISIN number.

Users can use the below regular expression to validate the ISIN number.

^[A-Z]{2}[-]{0,1}[A-Z0-9]{9}[-]{0,1}[0-9]{1}$
  • ^ − Start of the regular expression.

  • [A-Z]{2} − Country code of two characters.

  • [-]{0,1} − It may contain hyphen(-).

  • [A-Z0-9]{9} − Identifier of length 9 containing the characters from A to Z and 0 to 9.

  • [0-9]{1} − One checksum digit at last.

  • $ − End of the regular expression.

Approach 1

In this approach, we will use the ‘regex’ library of C++ to create the regular expression pattern from the string and regex_match() method to validate the ISIN number.

Algorithm

  • Step 1 − Define the ‘isinPat’ named regular expression as explained above.

  • Step 2 − Print' invalid string' if the string is empty.

  • Step 3 − Execute the regex_match() method after passing the ISIN number string as the first parameter and the search pattern as a second parameter.

  • Step 4 − Print ‘Yes’ if the ISIN string is valid. Otherwise, print ‘No’.

Example

#include <bits/stdc++.h>
#include <regex>
using namespace std;

void ISINValidation(string isinStr) {
   // Define the ReGex pattern for isinStr
   const regex isinPat("^[A-Z]{2}[-]{0,1}[A-Z0-9]{9}[-]{0,1}[0-9]{1}$");
   
   // For empty string
   if (isinStr.empty()) {
      cout << "The ISIN string is empty\n";
   }
   
   // Matching the string with regex
   if (regex_match(isinStr, isinPat)) {
      cout << "The ISIN string " << isinStr << " is valid\n";
   } else {
      cout << "The ISIN string " << isinStr << " is invalid\n";
   }
}
int main() {
   string str1 = "SB0123456A98";
   ISINValidation(str1);
   string str2 = "US-01207199D-8";
   ISINValidation(str2);
   return 0;
}

Output

The ISIN string SB0123456A98 is valid
The ISIN string US-01207199D-8 is valid

Time complexity – O(N), which is the same as the regex_match() method’s time complexity.

Space complexity – O(1)

Approach 2

This approach uses the regex_search() method to validate the ISIN number. The regex_search() method returns true when it finds the first match of the pattern in the string.

Algorithm

  • Step 1 − Return false for an empty string.

  • Step 2 − Validate the ISIN string with the regex_search() method.

  • Step 3 − Return true from the function if the ISIN string is valid. Otherwise, return false.

Example

#include <bits/stdc++.h>
#include <regex>
using namespace std;

bool ISINValidation(string isinStr) {
   // Define the ReGex pattern for isinStr
   const regex isinPat("^[A-Z]{2}[-]{0,1}[A-Z0-9]{9}[-]{0,1}\\d$");
   
   // For empty string
   if (isinStr == "") {
      return false;
   }
   
   // Matching the string with regex
   if (regex_search(isinStr, isinPat)) {
      return false;
   } else {
      return true;
   }
}
int main() {
   string str1 = "SB0123456A98";
   if (ISINValidation(str1)) {
      cout << "The ISIN number " << str1 << " is valid" << endl;
   } else {
      cout << "The ISIN number " << str1 << " is not valid" << endl;
   }
   return 0;
}

Output

The ISIN number SB0123456A98 is not valid

Time complexity – O(N) to search for the pattern in the string.

Space complexity – O(1)

The regex_match() matches the full string with the pattern, and regex_search()returns true for the partial match. So, in some cases, the regex_search() method can give the wrong output.

Updated on: 31-Aug-2023

256 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements