Skip to content
geeksforgeeks
  • 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
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
How to validate MAC address using Regular Expression
Next article icon

How to Validate MICR Code using Regular Expression?

Last Updated : 16 Dec, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

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 Recognition(MICR) Code:

  1. It is a 9-digit code.
  2. It should be in numeric form only.
  3. It should not contain any special characters

MICR comprises 3 parts:

  1. 1st Three digits specify the city code.
  2. The next three digits specify the Bank Code.
  3. The last three indicate the branch code

Note: Similarly, to validate IFSC Code using Regular Expression please refer to this article How to validate IFSC Code using Regular Expression

Examples of Correct MICR Codes

Input: str=”BNZAA2318J”
Output: false
Explanation: As it contains alphabets and the length is not equal to 9.
Input: str1=”123@3459″
Output: False
Explanation: It has a unique character that is against the property of the MICR code.

Input: str2=”9345268″
Output: false
Explanation: As its length is not equal to 9

Input: str3=”934517865″
Output: true
Explanation:

Where,
934 – Indicates the city code 
517 – Indicates the Bank Code
865 – Indicates the Branch Code

Approach

This problem can be dealt with Regular Expression. Regex will validate the entered data and will provide the exact format. Below are steps that can be taken for the problem:

  • Accept the string
  • Create a regex pattern to validate the MICR code As written below:   
regex="^[0-9]{1,9}$"
  • Where,
    • ^: Beginning of the string
    • [0-9]: match any character in the set
    • {1,9}: match Between 1 to 9 of the preceding token
    • $: End of the string

Below is the implementation of the above approach: 

C++




// C++ program to validate the
// MICR Code using Regular
// Expression
 
#include <iostream>
#include <regex>
using namespace std;
 
// Function to validate the
// MICR Code
bool isValidMICRCode(string mICRCode)
{
 
    // Regex to check valid
    // MICR Code.
    const regex pattern("^[0-9]{1,9}$");
 
    // If the MICR Code
    // is empty return false
    if (mICRCode.empty()) {
        return false;
    }
 
    // Return true if the MICR Code
    // matched the ReGex
    if (regex_match(mICRCode, pattern)) {
        return true;
    }
    else {
        return false;
    }
}
 
// Driver Code
int main()
{
    // Test Case 1:
    string str1 = "BNZAA2318J";
    cout << isValidMICRCode(str1) << endl;
 
    // Test Case 2:
    string str2 = "123@3459";
    cout << isValidMICRCode(str2) << endl;
 
    // Test Case 3:
    string str3 = "BNZAA2318JM";
    cout << isValidMICRCode(str3) << endl;
 
    // Test Case 4:
    string str4 = "934517865";
    cout << isValidMICRCode(str4) << endl;
 
    // Test Case 5:
    string str5 = "Rahul 1998";
    cout << isValidMICRCode(str5) << endl;
 
    // Test Case 6:
    string str6 = "654294563";
    cout << isValidMICRCode(str6) << endl;
 
    return 0;
}
 
 

Java




// Java program to validate the
// MICR Code  using Regular Expression
 
import java.util.regex.*;
 
class GFG
{
 
   // Function to validate the
   // MICR Code(For India Country Only)
   public static boolean isValidMICRCode(String MICRCode)
   {
 
       // Regex to check valid MICR Code
       String regex = "^[0-9]{1,9}$";
 
       // Compile the ReGex
       Pattern p = Pattern.compile(regex);
 
       // If the MICR Code
       // is empty return false
       if (MICRCode == null)
       {
           return false;
       }
 
       // Pattern class contains matcher() method
       // to find matching between given
       // MICR Code using regular expression.
       Matcher m = p.matcher(MICRCode);
 
       // Return if the MICR Code
       // matched the ReGex
       return m.matches();
   }
 
   // Driver Code.
   public static void main(String args[])
   {
 
       // Test Case 1:
       String str1 = "BNZAA2318J";
       System.out.println(isValidMICRCode(str1));
 
       // Test Case 2:
       String str2 = "123@3459";
       System.out.println(isValidMICRCode(str2));
 
       // Test Case 3:
       String str3 = "BNZAA2318JM";
       System.out.println(isValidMICRCode(str3));
 
       // Test Case 4:
       String str4 = "934517865";
       System.out.println(isValidMICRCode(str4));
 
       // Test Case 5:
       String str5 = "Rahul 1998";
       System.out.println(isValidMICRCode(str5));
      
       // Test Case 6:
       String str6 = "654294563";
       System.out.println(isValidMICRCode(str6));
 
   }
}
 
 

Python




# Python3 program to validate
# MICR Code  using Regular Expression
import re
 
# Function to validate
# MICR Code(For India Country Only)
def isValidMICRCode(str):
 
    # Regex to check valid MICR Code
    regex = "^[0-9]{1,9}$"
     
    # 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
 
# Test Case 1:
str1 = "BNZAA2318J"
print(isValidMICRCode(str1))
 
# Test Case 2:
str2 = "123@3459"
print(isValidMICRCode(str2))
 
# Test Case 3:
str3 = "Rahul 1998"
print(isValidMICRCode(str3))
 
# Test Case 4:
str4 = "934517865"
print(isValidMICRCode(str4))
 
# Test Case 5:
str5 = "BNZAA2318JM"
print(isValidMICRCode(str5))
 
# Test Case 6:
str6 = "654294563"
print(isValidMICRCode(str6))
 
# This code is contributed by Rahul Chauhan
 
 

C#




// C# program to validate the
// MICR Code
//using Regular Expressions
using System;
using System.Text.RegularExpressions;
class GFG
{
 
  // Main Method
  static void Main(string[] args)
  {
 
    // Input strings to Match
    // MICR Code
    string[] str={"BNZAA2318J","123@3459" ,
                  "BNZAA2318JM","934517865",
                  "Rahul 1998","654294563"};
    foreach(string s in str) {
      Console.WriteLine( isValidMICRCode(s) ? "true" : "false");
    }
    Console.ReadKey(); }
 
  // method containing the regex
  public static bool isValidMICRCode(string str)
  {
    string strRegex = @"^[0-9]{1,9}$";
    Regex re = new Regex(strRegex);
    if (re.IsMatch(str))
      return (true);
    else
      return (false);
  }
}
 
// This code is contributed by Rahul Chauhan
 
 

Javascript




// Javascript program to validate
// MICR Code  using Regular Expression
 
// Function to validate the
// MICR Code 
function isValidMICR_CODE(MICR_CODE) {
    // Regex to check valid
    // MICR CODE
    let regex = new RegExp(/^[0-9]{1,9}$/);
 
    // MICR CODE
    // is empty return false
    if (MICR_CODE == null) {
        return "false";
    }
 
    // Return true if the NUMBERPLATE
    // matched the ReGex
    if (regex.test(MICR_CODE) == true) {
        return "true";
    }
    else {
        return "false";
    }
}
 
// Driver Code
// Test Case 1:
let str1 = "UP 50 BY 1998";
console.log(isValidMICR_CODE(str1));
 
// Test Case 2:
let str2 = "MH 05 DL 9023";
console.log(isValidMICR_CODE(str2));
 
// Test Case 3:
let str3 = "BNZAA2318JM";
console.log(isValidMICR_CODE(str3));
 
// Test Case 4:
let str4 = "MH 05 S 9954";
console.log(isValidMICR_CODE(str4));
 
// Test Case 5:
let str5 = "934517865";
console.log(isValidMICR_CODE(str5));
 
// Test Case 6:
let str6 = "MH 05 DL 9023";
console.log(isValidMICR_CODE(str6));
 
// This code is contributed by Rahul Chauhan
 
 
Output
false false false true false true

Time Complexity: O(N) for each testcase, where N is the length of the given string. 
Auxiliary Space: O(1)  



Next Article
How to validate MAC address using Regular Expression
author
rahul_chauhan_1998
Improve
Article Tags :
  • C++
  • Java
  • regular-expression
Practice Tags :
  • CPP
  • Java

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 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
  • 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 ISIN using Regular Expressions
    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 sati
    6 min read
  • How to validate Visa Card number using Regular Expression
    Given a string str, the task is to check whether the given string is a valid Visa Card number or not by using Regular Expression. The valid Visa Card number must satisfy the following conditions: It should be 13 or 16 digits long, new cards have 16 digits and old cards have 13 digits.It should start
    6 min read
  • How to validate HTML tag using Regular Expression
    Given string str, the task is to check whether it is a valid HTML tag or not by using Regular Expression.The valid HTML tag must satisfy the following conditions: It should start with an opening tag (<).It should be followed by a double quotes string or single quotes string.It should not allow on
    6 min read
  • How to validate a domain name using Regular Expression
    Given string str, the task is to check whether the given string is a valid domain name or not by using Regular Expression.The valid domain name must satisfy the following conditions: The domain name should be a-z or A-Z or 0-9 and hyphen (-).The domain name should be between 1 and 63 characters long
    6 min read
  • How to validate MasterCard number using Regular Expression
    Given string str, the task is to check whether the given string is a valid Master Card number or not by using Regular Expression. The valid Master Card number must satisfy the following conditions. It should be 16 digits long.It should start with either two digits numbers may range from 51 to 55 or
    7 min read
  • Regular Expressions to Validate ISBN Code
    Given some ISBN Codes, the task is to check if they are valid or not using regular expressions. Rules for the valid codes are: It is a unique 10 or 13-digit.It may or may not contain a hyphen.It should not contain whitespaces and other special characters.It does not allow alphabet letters. Examples:
    5 min read
  • How to Validate a Password using Regular Expressions in Android?
    Regular Expression basically defines a search pattern, pattern matching, or string matching. It is present in java.util.regex package. Java Regex API provides 1 interface and 3 classes. They are the following: MatchResult InterfaceMatcher classPattern classPatternSyntaxException class Pattern p = Pa
    4 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