📌  相关文章
📜  如何使用正则表达式验证 IFSC 代码

📅  最后修改于: 2021-09-04 07:55:02             🧑  作者: Mango

给定字符串str ,任务是使用正则表达式检查给定字符串是否是有效的 IFSC(印度金融系统)代码。
有效的 IFSC(印度金融体系)代码必须满足以下条件:

  1. 它应该是 11 个字符长。
  2. 前四个字符应为大写字母。
  3. 第五个字符应该是 0。
  4. 最后六个字符通常是数字,但也可以是字母。

例子:

做法:思路是用正则表达式来解决这个问题。可以按照以下步骤计算答案。

  • 获取字符串。
  • 创建一个正则表达式来检查有效的 IFSC(印度金融系统)代码,如下所述:
regex = "^[A-Z]{4}0[A-Z0-9]{6}$";
  • 在哪里:
    • ^代表字符串的开始。
    • [AZ]{4}表示前四个字符应该是大写字母。
    • 0代表第五个字符应该是 0。
    • [A-Z0-9]{6}表示接下来的六个字符,通常是数字,但也可以是字母。
    • $表示字符串的结尾。

下面是上述方法的实现:

C++
// C++ program to validate the
// IFSC (Indian Financial System) Code using Regular Expression
#include 
#include 
using namespace std;
 
// Function to validate the IFSC (Indian Financial System) Code.
bool isValidIFSCode(string str)
{
 
  // Regex to check valid IFSC (Indian Financial System) Code.
  const regex pattern("^[A-Z]{4}0[A-Z0-9]{6}$");
 
 
  // If the IFSC (Indian Financial System) Code
  // is empty return false
  if (str.empty())
  {
     return false;
  }
 
  // Return true if the IFSC (Indian Financial System) Code
  // matched the ReGex
  if(regex_match(str, pattern))
  {
    return true;
  }
  else
  {
    return false;
  }
}
 
// Driver Code
int main()
{
   
  // Test Case 1:
  string str1 = "SBIN0125620";
  cout << boolalpha << isValidIFSCode(str1) << endl;
 
  // Test Case 2:
  string str2 = "SBIN0125";
  cout << boolalpha << isValidIFSCode(str2) << endl;
 
  // Test Case 3:
  string str3 = "1234SBIN012";
  cout << boolalpha << isValidIFSCode(str3) << endl;
 
  // Test Case 4:
  string str4 = "SBIN7125620";
  cout << boolalpha <


Java
// Java program to validate
// IFSC (Indian Financial System) Code
// using regular expression.
import java.util.regex.*;
class GFG {
 
    // Function to validate
    // IFSC (Indian Financial System) Code
    // using regular expression.
    public static boolean isValidIFSCode(String str)
    {
        // Regex to check valid IFSC Code.
        String regex = "^[A-Z]{4}0[A-Z0-9]{6}$";
 
        // Compile the ReGex
        Pattern p = Pattern.compile(regex);
 
        // If the string is empty
        // return false
        if (str == null) {
            return false;
        }
 
        // Pattern class contains matcher()
        // method to find matching between
        // the given string and
        // the regular expression.
        Matcher m = p.matcher(str);
 
        // Return if the string
        // matched the ReGex
        return m.matches();
    }
 
    // Driver Code.
    public static void main(String args[])
    {
 
        // Test Case 1:
        String str1 = "SBIN0125620";
        System.out.println(isValidIFSCode(str1));
 
        // Test Case 2:
        String str2 = "SBIN0125";
        System.out.println(isValidIFSCode(str2));
 
        // Test Case 3:
        String str3 = "1234SBIN012";
        System.out.println(isValidIFSCode(str3));
 
        // Test Case 4:
        String str4 = "SBIN7125620";
        System.out.println(isValidIFSCode(str4));
    }
}


Python3
# Python3 program to validate
# IFSC (Indian Financial System) Code 
# using regular expression
import re
 
# Function to validate
# IFSC (Indian Financial System) Code
# using regular expression.
def  isValidIFSCode(str):
 
    # Regex to check valid IFSC Code.
    regex = "^[A-Z]{4}0[A-Z0-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
 
# Test Case 1:
str1 = "SBIN0125620"
print(isValidIFSCode(str1))
 
# Test Case 2:
str2 = "SBIN0125"
print(isValidIFSCode(str2))
 
# Test Case 3:
str3 = "1234SBIN012"
print(isValidIFSCode(str3))
 
# Test Case 4:
str4 = "SBIN7125620"
print(isValidIFSCode(str4))
 
# This code is contributed by avanitrachhadiya2155


Java
// Java program to validate
// IFSC (Indian Financial System) Code
// using regular expression.
class GFG {
 
    // Function to validate
    // IFSC (Indian Financial System) Code
    // using regular expression.
    public static boolean isValidIFSCode(String str)
    {
        // Regex to check valid IFSC Code.
        String regex = "^[A-Z]{4}0[A-Z0-9]{6}$";
        return str.trim().matches(regex);
    }
 
    // Driver Code.
    public static void main(String args[])
    {
 
        // Test Case 1:
        String str1 = "SBIN0125620";
        System.out.println(isValidIFSCode(str1));
 
        // Test Case 2:
        String str2 = "SBIN0125";
        System.out.println(isValidIFSCode(str2));
 
        // Test Case 3:
        String str3 = "1234SBIN012";
        System.out.println(isValidIFSCode(str3));
 
        // Test Case 4:
        String str4 = "SBIN7125620";
        System.out.println(isValidIFSCode(str4));
    }
}


输出
true
false
false
false

使用 String.matches() 方法

此方法告诉此字符串是否与给定的正则表达式匹配。对表单的此方法的调用 str.matches(regex) 产生与表达式Pattern.matches(regex, str)完全相同的结果 Pattern.compile(regex) 编译模式,这样当你执行 Matcher.matches() 时,模式不会一次又一次地重新编译。 Pattern.compile 预编译它。但是,如果您使用字符串.matches,它会在每次执行此行时编译模式。所以,最好使用 Pattern.compile()。

Java

// Java program to validate
// IFSC (Indian Financial System) Code
// using regular expression.
class GFG {
 
    // Function to validate
    // IFSC (Indian Financial System) Code
    // using regular expression.
    public static boolean isValidIFSCode(String str)
    {
        // Regex to check valid IFSC Code.
        String regex = "^[A-Z]{4}0[A-Z0-9]{6}$";
        return str.trim().matches(regex);
    }
 
    // Driver Code.
    public static void main(String args[])
    {
 
        // Test Case 1:
        String str1 = "SBIN0125620";
        System.out.println(isValidIFSCode(str1));
 
        // Test Case 2:
        String str2 = "SBIN0125";
        System.out.println(isValidIFSCode(str2));
 
        // Test Case 3:
        String str3 = "1234SBIN012";
        System.out.println(isValidIFSCode(str3));
 
        // Test Case 4:
        String str4 = "SBIN7125620";
        System.out.println(isValidIFSCode(str4));
    }
}
输出
true
false
false
false

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live