Validate Corporate Identification Number (CIN) using Regular Expression
Last Updated :
27 Jan, 2023
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++
#include <bits/stdc++.h>
#include <regex>
using namespace std;
string isValid_CIN_Number(string str)
{
const regex pattern(
"([LUu]{1})([0-9]{5})([A-Za-z]{2})([0-9]{4})([A-Za-"
"z]{3})([0-9]{6})$" );
if (str.empty()) {
return "false" ;
}
if (regex_match(str, pattern)) {
return "true" ;
}
else {
return "false" ;
}
}
int main()
{
string str1 = "U12345AB6784CDE123456" ;
cout << isValid_CIN_Number(str1) << endl;
string str2 = "L12345AB6784CDE123456" ;
cout << isValid_CIN_Number(str2) << endl;
string str3 = "u12345AB6784CDE123456" ;
cout << isValid_CIN_Number(str3) << endl;
string str4 = "L12345@&^6784CDE123456" ;
cout << isValid_CIN_Number(str4) << endl;
string str5 = "U12345AB6784CDE1234" ;
cout << isValid_CIN_Number(str5) << endl;
return 0;
}
|
Java
import java.util.regex.*;
class GFG {
public static boolean isValid_CIN_Number(String str)
{
String regex
= "^([LUu]{1})([0-9]{5})([A-Za-z]{2})([0-9]{4})([A-Za-z]{3})([0-9]{6})$" ;
Pattern p = Pattern.compile(regex);
if (str == null ) {
return false ;
}
Matcher m = p.matcher(str);
return m.matches();
}
public static void main(String args[])
{
String str1 = "U12345AB6784CDE123456" ;
System.out.println(isValid_CIN_Number(str1));
String str2 = "L12345AB6784CDE123456" ;
System.out.println(isValid_CIN_Number(str2));
String str3 = "u12345AB6784CDE123456" ;
System.out.println(isValid_CIN_Number(str3));
String str4 = "L12345@&^6784CDE123456" ;
System.out.println(isValid_CIN_Number(str4));
String str5 = "U12345AB6784CDE1234" ;
System.out.println(isValid_CIN_Number(str5));
}
}
|
Python3
import re
def isValid_CIN_Number( str ):
regex = "([LUu]{1})([0-9]{5})([A-Za-z]{2})([0-9]{4})([A-Za-z]{3})([0-9]{6})$"
p = re. compile (regex)
if ( str = = None ):
return "false"
if (re.search(p, str )):
return "true"
else :
return "false"
if __name__ = = '__main__' :
str1 = "U12345AB6784CDE123456"
print (isValid_CIN_Number(str1))
str2 = "L12345AB6784CDE123456"
print (isValid_CIN_Number(str2))
str3 = "U12345AB6784CDE123456"
print (isValid_CIN_Number(str3))
str4 = "L12345@&^6784CDE123456"
print (isValid_CIN_Number(str4))
str5 = "U12345AB6784CDE1234"
print (isValid_CIN_Number(str5))
|
C#
using System;
using System.Text.RegularExpressions;
public class GFG {
public static bool isValid_CIN_Number( string str)
{
string regex
= "^([LUu]{1})([0-9]{5})([A-Za-z]{2})([0-9]{4})([A-Za-z]{3})([0-9]{6})$" ;
Regex p = new Regex(regex);
if (str == null ) {
return false ;
}
Match m = p.Match(str);
return m.Success;
}
public static void Main()
{
string str1 = "U12345AB6784CDE123456" ;
Console.WriteLine(isValid_CIN_Number(str1));
string str2 = "L12345AB6784CDE123456" ;
Console.WriteLine(isValid_CIN_Number(str2));
string str3 = "u12345AB6784CDE123456" ;
Console.WriteLine(isValid_CIN_Number(str3));
string str4 = "L12345@&^6784CDE123456" ;
Console.WriteLine(isValid_CIN_Number(str4));
string str5 = "U12345AB6784CDE1234" ;
Console.WriteLine(isValid_CIN_Number(str5));
}
}
|
Javascript
function isValid_CIN_Number(str) {
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 == null ) {
return "false" ;
}
if (regex.test(str) == true ) {
return "true" ;
}
else {
return "false" ;
}
}
let str1 = "U12345AB6784CDE123456" ;
console.log(isValid_CIN_Number(str1));
let str2 = "L12345AB6784CDE123456" ;
console.log(isValid_CIN_Number(str2));
let str3 = "u12345AB6784CDE123456" ;
console.log(isValid_CIN_Number(str3));
let str4 = "L12345@&^6784CDE123456" ;
console.log(isValid_CIN_Number(str4));
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:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...