ホームページ  >  記事  >  バックエンド開発  >  正規表現を使用して IFSC コードを検証するにはどうすればよいですか?

正規表現を使用して IFSC コードを検証するにはどうすればよいですか?

WBOY
WBOY転載
2023-08-26 22:17:081237ブラウズ

正規表現を使用して IFSC コードを検証するにはどうすればよいですか?

インド金融システムコードは略語です。電子資金移動システムに参加しているインドの銀行支店は、特別な 11 文字のコードで識別されます。インド準備銀行は、インターネット取引でこのコードを使用して銀行間で資金を送金します。 IFSC コードは 2 つの部分に分かれています。銀行は最初の 4 文字で識別され、支店は最後の 6 文字で識別されます。 NEFT (National Electronic Funds Transfer)、RTGS (Real Time Gross Settlement)、および IMPS (Immediate Payment Service) は、IFSC コードを必要とする電子トランザクションの一部です。

###方法###

正規表現を使用して IFSC コードを検証する一般的な方法は次のとおりです。

    長さが正しいか確認してください。
  • 最初の 4 文字を確認してください。
  • 5 文字目を確認してください。
  • 最後の 6 文字を確認してください。
  • 方法 1: 正しい長さを確認する

11 文字が IFSC コードを構成する必要があります。長さを決定するには、次の正規表現 −

を使用します。 リーリー

この正規表現は、任意の 11 文字に一致します。

###文法###

正規表現を使用して IFSC コードを確認します。構文を使用して正しい長さを確認できます −

リーリー

^
    文字列の先頭をマークします
  • ([A-Z]{4}
  • IFSC コードの最初の 4 文字と一致します。大文字である必要があります
  • [0]
  • IFSC コードの 5 番目の文字と一致します (ゼロである必要があります)
  • [A-Z0-9]{6}
  • IFSC コードの最後の 6 文字と一致します。大文字または数字のいずれかである必要があります。
  • $
  • 文字列の終わりをマークします この正規表現は、IFSC コードに 4 つの大文字、0、および 6 つの大文字または数字を含む 11 文字が含まれていることを保証します。

    ###アルゴリズム###
  • 正規表現を利用して IFSC コードの長さを検証する詳細な手順は次のとおりです -

ステップ 1

- IFSC コードの正規表現パターンを説明します: IFSC コードは 11 文字の英数字コードで、銀行コードは最初の 4 文字で表され、支店コードは最後の 6 文字で表されます。 、および常にゼロの 5 番目の文字 IFSC コードの正規表現パターンは次のとおりです-

リーリー

ステップ 2

- 正規表現パターンを確認する: regex101.com や regexr.com などのオンライン正規表現テスト ツールを使用して、正規表現パターンをテストできます。パターンをテスト ツールに入力し、IFSC コードを入力してパターンと一致するかどうかを確認します。

ステップ 3

-IFSC コードの長さを確認する:パターン テストを実施した後、IFSC コードの長さを確認する必要があります。 Python の len() メソッドを使用すると、IFSC コードが必要な長さ (11 文字) であるかどうかを判断できます。

ステップ 4

- 正規表現パターンを使用する: 長さを決定した後、正規表現パターンを使用して、IFSC コードが期待どおりにフォーマットされているかどうかを判断できます。このパターンを Python の IFSC コードに適用するには、re モジュールを使用します。 例 1

この場合、IFSC コードは正規表現 [A-Z]40[A-Z0-9]6$ を使用して検証されます。正規表現は次のパターンに一致します -

コードの最初の 4 文字 ([A-Z]) は大文字である必要があります。

    数字のゼロ (0) は 5 番目の文字である必要があります。
  • 最後の 6 文字 ([A-Z0-9]6
  • $

    ] には大文字または数字を使用できます。

  • ifsc_code 文字列と正規表現を照合するには、regex_match 関数を使用します。文字列が正規表現と一致する場合、コードは有効であるとみなされます。一致しない場合は無効とみなされます。
  • リーリー ###出力### リーリー

    方法 2: 最初の 4 文字を確認する IFSC コードの最初の 4 文字は銀行を識別します。正規表現を使用して、最初の 4 文字がアルファベットであることを確認できます。

    リーリー
  • この正規表現は、任意の 4 つの大文字と一致します。
###文法###

これは、IFSC コードの最初の 4 文字をチェックするための正規表現です -

リーリー

この正規表現では次の構文を使用します -

#^

文字列の先頭と一致します。

[A-Z]

任意の大文字と一致します。

  • {4} 前述のパターンがちょうど 4 回出現することを指定します。

  • () 一致したテキストを抽出するためのキャプチャ グループを作成します。

  • この正規表現は、4 つの大文字で始まる任意の文字列と一致します。 IFSC コード全体を検証するには、最初の 4 文字以外の条件をチェックする必要があります。

    ###アルゴリズム### 正規表現を使用して IFSC コードの最初の 4 文字を検証するための段階的なアルゴリズムを次に示します -

    步骤1 − 为IFSC代码的前四个字符指定正则表达式模式。前四个字符应仅使用字母,其中前两个字符代表银行代码,后两个字符代表位置代码。可以用正则表达式表示为[A-Z]4。

    Step 2 − Obtain the input IFSC code that requires validation.

    第三步 - 删除提供的IFSC代码的前四个字符。

    Step 4 − Verify whether the extracted first four characters fit the specified pattern using the regular expression match () function. The input IFSC code is regarded as valid if the match is successful and the validation is successful. If there is no match, the validation is unsuccessful and the input IFSC code is deemed invalid.

    Note: This algorithm only checks the first four characters of the IFSC code. The complete validation of the IFSC code requires additional checks for the remaining characters.

    Example 2

    In this illustration, the IFSC code we want to validate is represented by the string "ifsc_code." Then, in accordance with the IFSC code format, we build a regular expression pattern using the std::regex class that matches any string that begins with four letters.

    然后,使用std::regex_search函数检查ifsc_code字符串是否与正则表达式模式匹配。如果匹配成功,则输出一个通知,说明IFSC代码是合法的。如果不匹配,则输出一个通知,说明IFSC代码无效。

    #include <iostream>
    #include <regex>
    
    int main() {
       std::string ifsc_code = "ABCD123456";
       std::regex pattern("^[A-Za-z]{4}");
      
       if (std::regex_search(ifsc_code, pattern)) {
          std::cout << "IFSC code is valid." << std::endl;
       } else {
          std::cout << "IFSC code is invalid." << std::endl;
       }
       return 0;
    }
    

    Output

    IFSC code is valid.
    

    Method 3: Check the fifth character

    The fifth character of the IFSC Code is a zero (0) and is reserved for future use. One can use a regular expression to check that the fifth character is a zero.

    ^.{4}0
    

    这个正则表达式匹配任意四个字符后面跟着一个零。

    语法

    To check the fifth character and validate an IFSC code using a regular expression, you can use the following general syntax −

    ^[A-Z]{4}[0]{1}[A-Z0-9]{6}$
    
    • ^ and $  represent the start and end of the string, respectively, ensuring that the entire string matches the pattern.

    • [A-Z]{4}  匹配正好四个大写字母字符。这表示银行代码。

    • [0]{1} 匹配正好一个零。这代表了IFSC代码中的第五个字符。

    • [A-Z0-9]{6} 匹配恰好六个字符,可以是大写字母或数字。这代表分行代码。

    • 总的来说,该模式匹配以四个大写字母开头,后跟一个零,并以六个大写字母或数字结尾的IFSC代码。

    算法

    这里有一个使用正则表达式检查IFSC代码第五个字符的算法 -

    步骤 1 − 输入 IFSC 代码。

    Step 2 − Define the regular expression pattern for IFSC codes: "^.{4}.{1}.*$"

    Step 3 − Use the regular expression pattern to match the input IFSC code.

    Step 4 − If there is a match −

    • 获取IFSC代码的第五个字符。

    • Check if the fifth character is valid according to your criteria (e.g., a specific range of characters, specific characters, etc.).

    • If the fifth character is valid: - Output "IFSC code is valid."

    • If the fifth character is not valid: - Output "IFSC code is not valid."

    第五步 - 如果没有匹配 -

    • Output "IFSC code is not valid."

    Example 3

    的中文翻译为:

    示例 3

    一个在C++中的示例,展示了如何利用正则表达式来检查IFSC代码的第五个字符,而不需要用户输入

    在这个例子中,IFSC代码“SBIN0001234”被用作样本代码。为了匹配IFSC代码的结构,使用了一个正则表达式模式[A-Za-z]40[A-Z0-9]6$。提取第五个字符,然后验证代码是否符合该模式。如果第五个字符是大写字母,则被接受。否则,它是无效的。

    #include <iostream>
    #include <regex>
    
    int main() {
       std::string ifscCode = "SBIN0001234"; // Example IFSC code
    
       // Regular expression pattern to match IFSC code
       std::regex pattern("^[A-Za-z]{4}0[A-Z0-9]{6}$");
    
       // Check if the IFSC code matches the pattern
       if (std::regex_match(ifscCode, pattern)) {
          // Extract the fifth character
          char fifthCharacter = ifscCode[4];
    
          // Perform validation on the fifth character
          if (std::isalpha(fifthCharacter) && std::isupper(fifthCharacter)) {
             std::cout << "Fifth character is valid: " << fifthCharacter << std::endl;
          } else {
             std::cout << "Fifth character is invalid: " << fifthCharacter << std::endl;
          }
       } else {
          std::cout << "Invalid IFSC code." << std::endl;
       }
       return 0;
    }
    

    Output

    Fifth character is invalid: 0
    

    Method 4: Check the last six characters

    IFSC代码的最后六个字符标识分支机构。您可以使用正则表达式来检查最后六个字符是否为字母数字。

    ^.{4}[A-Z0-9]{6}$
    

    This regular expression matches any four characters followed by six alphanumeric characters.

    By combining the above regular expressions, you can create a regular expression to validate the entire IFSC Code.

    ^[A-Z]{4}0[A-Z0-9]{6}$
    

    这个正则表达式匹配任何有效的IFSC代码。

    语法

    The regular expression pattern ^[A-Z]{4}\d{6}$ consists of the following components −

    • ^ indicates the start of the string.

    • [A-Z]{4} 匹配正好四个大写字母字符。

    • \d{6} 匹配正好六个数字。

    • $ indicates the end of the string.

    算法

    使用正则表达式检查IFSC代码的最后六个字符,您可以按照以下算法进行操作 -

    步骤 1 − 定义一个正则表达式模式,该模式匹配 IFSC 编码的最后六个字符。例如,该模式可以是 "[A-Z0-9]{6}"。

    步骤 2 - 创建一个用于测试的样本 IFSC 代码列表。这些代码应该是有效的 IFSC 代码。

    第三步 - 对列表中的每个IFSC代码 -

    Extract the last six characters from the IFSC code.

    使用正则表达式模式来匹配提取的字符。

    If the match is successful, the last six characters are valid.

    If the match fails, the last six characters are not valid.

    第四步 - 打印每个IFSC代码的结果(有效或无效)。

    Example 4

    的中文翻译为:

    示例 4

    在这里,我们定义了一个正则表达式模式[A-Z0-9] $,它匹配任何一组大写字母(A-Z)或数字(0-9),恰好出现六次(6),在字符串的末尾($)。然后,为了检查ifscCode字符串是否与模式匹配,我们使用std::regex_match()。在这种情况下,我们发布"IFSC code is valid",而在没有匹配的情况下,我们打印"IFSC code invalid"。

    #include <iostream>
    #include <regex>
    
    int main() {
       std::string ifscCode = "SBIN0001234";  // Example IFSC code
    
       // Regular expression pattern to match the last six characters of an IFSC code
       std::regex pattern("[A-Z0-9]{6}$");
    
       // Checking if the last six characters of the IFSC code match the pattern
       if (std::regex_match(ifscCode, pattern)) {
          std::cout << "IFSC code is valid." << std::endl;
       } else {
          std::cout << "IFSC code is invalid." << std::endl;
       }
       return 0;
    }
    

    Output

    IFSC code is invalid.
    

    Conclusion

    总之,利用正则表达式来验证IFSC代码可以是一种实用且有效的技术,以确保代码的格式正确。任何不符合所需模式的输入都可以使用正则表达式标记为无效,以定义IFSC代码必须遵循的模式。

    Prior to applying regular expressions to validate an IFSC code, it's critical to comprehend the format and structure of the code. The bank code is represented by the first four characters of the IFSC code, the branch code by the next six characters, and the zero as the fifth character.

以上が正規表現を使用して IFSC コードを検証するにはどうすればよいですか?の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事はtutorialspoint.comで複製されています。侵害がある場合は、admin@php.cn までご連絡ください。