Skip to content
Related Articles
Open in App
Not now

Related Articles

Validate Corporate Identification Number (CIN) using Regular Expression

Improve Article
Save Article
  • Last Updated : 27 Jan, 2023
Improve Article
Save Article

Given some  Corporate Identification Number, the task is to check if they are valid or not using regular expressions. Rules for the valid CIN are: 

  • CIN is a 21 digits alpha-numeric code.
    • It starts with either alphabet letter U or L.
    • Next five characters are reserved for digits (0-9).
    • Next two places are occupied by alphabet letters(A-Z-a-z).
    • Next four places are taken by digits(0-9).
    • Next three characters are reserved for alphabet letters (A-Za-z).
    • Next six characters are digits(0-9).
  • It should not contain any special character or whitespaces.

Examples:

Input: str= ”U12345AB6784CDE123456”
Output: True
Explanation: It is matching with the above mentioned points.

Input: str= ”R12345AB6784CDE123456”
Output: False
Explanation: A CIN number must start with U or L.

Input: str= ”U12345AB6784CDE1234”
Output: False
Explanation: Its length should be equal to 21.

Approach: The problem can be solved based on the following idea:

Create a regex pattern to validate the number as written below:   

regex= ^([LUu]{1})([0-9]{5})([A-Za-z]{2})([0-9]{4})([A-Za-z]{3})([0-9]{6})$

Where, 
^ : Start of the string 
[LUu]{1} : This pattern will match one of the preceding item if it is either U, u or L.
[0-9]{5} : This pattern will match five of the preceding items if it is a digit(0-9).
[A-Z-a-z]{2}:  This pattern will match two of the preceding items if it in the range “A” to “Z” OR “a” to “z”.
$ : End of the String.

Follow the below steps to implement the idea:

  • Create a regex expression for Corporate Identification Number.
  • Use Pattern class to compile the regex formed.
  • Use the matcher function to check whether the CIN is valid or not.
  • If it is valid, return true. Otherwise, return false.

Below is the code implementation of the above approach:-

C++




// C++ program to validate the
// Corporate Identification Number (CIN)
// using Regular Expression
 
#include <bits/stdc++.h>
#include <regex>
using namespace std;
 
// Function to validate the
// Corporate Identification Number (CIN)
string isValid_CIN_Number(string str)
{
    // Regex to check valid
    // CIN Number
    const regex pattern(
        "([LUu]{1})([0-9]{5})([A-Za-z]{2})([0-9]{4})([A-Za-"
        "z]{3})([0-9]{6})$");
 
    // If the str is empty return false
    if (str.empty()) {
        return "false";
    }
 
    // Return true if the str
    // matched the ReGex
    if (regex_match(str, pattern)) {
        return "true";
    }
    else {
        return "false";
    }
}
 
// Driver Code
int main()
{
    // Test Case 1:
    string str1 = "U12345AB6784CDE123456";
    cout << isValid_CIN_Number(str1) << endl;
 
    // Test Case 2:
    string str2 = "L12345AB6784CDE123456";
    cout << isValid_CIN_Number(str2) << endl;
 
    // Test Case 3:
    string str3 = "u12345AB6784CDE123456";
    cout << isValid_CIN_Number(str3) << endl;
 
    // Test Case 4:
    string str4 = "L12345@&^6784CDE123456";
    cout << isValid_CIN_Number(str4) << endl;
 
    // Test Case 5:
    string str5 = "U12345AB6784CDE1234";
    cout << isValid_CIN_Number(str5) << endl;
 
    return 0;
}


Java




// Java program to validate the
// Corporate Identification Number (CIN)
// using Regular Expression
 
import java.util.regex.*;
 
class GFG {
 
    // Function to validate the
    // Corporate Identification Number (CIN)
    public static boolean isValid_CIN_Number(String str)
    {
        // Regex to check valid CIN Number
        String regex
            = "^([LUu]{1})([0-9]{5})([A-Za-z]{2})([0-9]{4})([A-Za-z]{3})([0-9]{6})$";
 
        // Compile the ReGex
        Pattern p = Pattern.compile(regex);
 
        // If the str
        // is empty return false
        if (str == null) {
            return false;
        }
 
        // Pattern class contains matcher()
        // methodto find matching between
        // given str using regex.
        Matcher m = p.matcher(str);
 
        // Return if the str
        // matched the ReGex
        return m.matches();
    }
 
    // Driver Code.
    public static void main(String args[])
    {
        // Test Case 1:
        String str1 = "U12345AB6784CDE123456";
        System.out.println(isValid_CIN_Number(str1));
 
        // Test Case 2:
        String str2 = "L12345AB6784CDE123456";
        System.out.println(isValid_CIN_Number(str2));
 
        // Test Case 3:
        String str3 = "u12345AB6784CDE123456";
        System.out.println(isValid_CIN_Number(str3));
 
        // Test Case 4:
        String str4 = "L12345@&^6784CDE123456";
        System.out.println(isValid_CIN_Number(str4));
 
        // Test Case 5:
        String str5 = "U12345AB6784CDE1234";
        System.out.println(isValid_CIN_Number(str5));
    }
}


Python3




# Python3 program to validate
# Corporate Identification Number (CIN)
# using Regular Expression
 
import re
 
 
# Function to validate
# Corporate Identification Number (CIN)
def isValid_CIN_Number(str):
 
    # Regex to check valid CIN Number
    regex = "([LUu]{1})([0-9]{5})([A-Za-z]{2})([0-9]{4})([A-Za-z]{3})([0-9]{6})$"
 
    # 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 = "U12345AB6784CDE123456"
    print(isValid_CIN_Number(str1))
     
    # Test Case 2:
    str2 = "L12345AB6784CDE123456"
    print(isValid_CIN_Number(str2))
     
    # Test Case 3:
    str3 = "U12345AB6784CDE123456"
    print(isValid_CIN_Number(str3))
     
    # Test Case 4:
    str4 = "L12345@&^6784CDE123456"
    print(isValid_CIN_Number(str4))
     
    # Test Case 5:
    str5 = "U12345AB6784CDE1234"
    print(isValid_CIN_Number(str5))


C#




// C# program to validate the
// Corporate Identification Number (CIN)
// using Regular Expression
 
using System;
using System.Text.RegularExpressions;
 
public class GFG {
 
  // Function to validate the
  // Corporate Identification Number (CIN)
  public static bool isValid_CIN_Number(string str)
  {
    // Regex to check valid CIN Number
    string regex
      = "^([LUu]{1})([0-9]{5})([A-Za-z]{2})([0-9]{4})([A-Za-z]{3})([0-9]{6})$";
 
    // Compile the ReGex
    Regex p = new Regex(regex);
 
    // If the str
    // is empty return false
    if (str == null) {
      return false;
    }
 
    // Pattern class contains matcher()
    // methodto find matching between
    // given str using regex.
    Match m = p.Match(str);
 
    // Return if the str
    // matched the ReGex
    return m.Success;
  }
 
  // Driver Code.
  public static void Main()
  {
    // Test Case 1:
    string str1 = "U12345AB6784CDE123456";
    Console.WriteLine(isValid_CIN_Number(str1));
 
    // Test Case 2:
    string str2 = "L12345AB6784CDE123456";
    Console.WriteLine(isValid_CIN_Number(str2));
 
    // Test Case 3:
    string str3 = "u12345AB6784CDE123456";
    Console.WriteLine(isValid_CIN_Number(str3));
 
    // Test Case 4:
    string str4 = "L12345@&^6784CDE123456";
    Console.WriteLine(isValid_CIN_Number(str4));
 
    // Test Case 5:
    string str5 = "U12345AB6784CDE1234";
    Console.WriteLine(isValid_CIN_Number(str5));
  }
}
 
// This code is contributed by Pushpesh Raj.


Javascript




// Javascript program to validate
// Corporate Identification Number (CIN) using Regular Expression
 
// Function to validate the
// Corporate Identification Number (CIN)
function isValid_CIN_Number(str) {
    // Regex to check valid
    // CIN Number
    let regex = new RegExp(/^([LUu]{1})([0-9]{5})([A-Za-z]{2})([0-9]{4})([A-Za-z]{3})([0-9]{6})$/);
 
    //if str
    // is empty return false
    if (str == null) {
        return "false";
    }
 
    // Return true if the str
    // matched the ReGex
    if (regex.test(str) == true) {
        return "true";
    }
    else {
        return "false";
    }
}
 
// Driver Code
// Test Case 1:
let str1 = "U12345AB6784CDE123456";
console.log(isValid_CIN_Number(str1));
 
// Test Case 2:
let str2 = "L12345AB6784CDE123456";
console.log(isValid_CIN_Number(str2));
 
// Test Case 3:
let str3 = "u12345AB6784CDE123456";
console.log(isValid_CIN_Number(str3));
 
// Test Case 4:
let str4 = "L12345@&^6784CDE123456";
console.log(isValid_CIN_Number(str4));
 
// Test Case 5:
let str5 = "U12345AB6784CDE1234";
console.log(isValid_CIN_Number(str5));


Output

true
true
true
false
false

Time Complexity : O(1)

Space Complexity : O(1)

Related Articles:


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!