Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • DSA
  • Practice Pattern Searching
  • Tutorial on Pattern Searching
  • Naive Pattern Searching
  • Rabin Karp
  • KMP Algorithm
  • Z Algorithm
  • Trie for Pattern Seaching
  • Manacher Algorithm
  • Suffix Tree
  • Ukkonen's Suffix Tree Construction
  • Boyer Moore
  • Aho-Corasick Algorithm
  • Wildcard Pattern Matching
Open In App
Next Article:
How to validate ISIN using Regular Expressions
Next article icon

How to validate ISIN using Regular Expressions

Last Updated : 02 Mar, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

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.
  3. ISIN code must start with alphabets only.
  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 <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 

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:

  • How to write Regular Expressions?

Next Article
How to validate ISIN using Regular Expressions

R

rahul_chauhan_1998
Improve
Article Tags :
  • Strings
  • Pattern Searching
  • Technical Scripter
  • DSA
  • Technical Scripter 2022
  • regular-expression
Practice Tags :
  • Pattern Searching
  • Strings

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 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
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences