搜索
首页后端开发C++如何使用正则表达式验证IFSC代码?

如何使用正则表达式验证IFSC代码?

印度金融系统代码是缩写。参与电子资金转移系统的印度银行分支机构由一个特殊的11位字符代码进行标识。印度储备银行在互联网交易中使用此代码在银行之间转移资金。IFSC代码分为两个部分。银行由前四个字符进行标识,而分支机构由最后六个字符进行标识。NEFT(全国电子资金转移)、RTGS(实时毛额结算)和IMPS(即时支付服务)是一些需要IFSC代码的电子交易。

Method

使用正则表达式验证IFSC代码的一些常见方法有:

  • 检查长度是否正确。

  • 检查前四个字符。

  • Check the fifth character.

  • Check the last six characters .

方法1:检查正确的长度

11 characters should make up the IFSC Code. To determine the length, use the following regular expression −

^.{11}$

This regular expression matches any 11 characters.

语法

使用正则表达式验证IFSC代码,可以使用语法来检查正确的长度−

^([A-Z]{4}[0][A-Z0-9]{6})$ 
  • ^  Marks the beginning of the string

  • ([A-Z]{4}  匹配IFSC代码的前4个字符,应为大写字母

  • [0]  匹配IFSC代码的第五个字符,应为零

  • [A-Z0-9]{6}  Matches the last 6 characters of IFSC code, which should be either uppercase letters or numbers.

  • $  Marks the end of the string

这个正则表达式保证了IFSC代码中包含11个字符,其中包括4个大写字母,一个零,然后是6个大写字母或数字。

算法

Here is a detailed procedure for utilising regular expressions to validate an IFSC code's length −

Step 1 − Describe the regular expression pattern for an IFSC code: An IFSC code is an 11-character alphanumeric code. The bank code is represented by first four characters, branch code by the last six characters, and always-zero fifth character. An IFSC code's regular expression pattern is as follows−

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

步骤2 - 检查正则表达式模式:可以使用在线正则表达式测试工具,如regex101.com和regexr.com来测试正则表达式模式。将模式输入到测试工具中,然后输入一个IFSC代码来检查是否与模式匹配。

步骤 3 − 验证IFSC代码的长度:在进行模式测试后,您必须验证IFSC代码的长度。Python中的len()方法可以用来确定IFSC代码是否是需要的确切长度,即11个字符。

第四步 - 使用正则表达式模式:在确定长度之后,您可以使用正则表达式模式来确定IFSC代码的格式是否符合预期。要在Python中将该模式应用于IFSC代码,请使用re模块。

Example 1

在这种情况下,IFSC代码使用正则表达式[A-Z]40[A-Z0-9]6$进行验证。正则表达式匹配以下模式−

  • 代码的前四个字母(从[A-Z])必须是大写。

  • The number zero (0) must be the fifth character.

  • 最后六个字符([A-Z0-9]6$] 可以是大写字母或数字。

使用regex_match函数匹配ifsc_code字符串和正则表达式。如果字符串与正则表达式匹配,则代码被认为是有效的。如果不匹配,则被认为是无效的。

#include <iostream>
#include <regex>

using namespace std;

int main() {
   string ifsc_code = "SBIN0000123"; // Example IFSC code
   regex ifsc_regex("^[A-Z]{4}0[A-Z0-9]{6}$"); // Regular expression for IFSC code
    
   if (regex_match(ifsc_code, ifsc_regex)) {
      cout << "Valid IFSC code\n";
   } else {
      cout << "Invalid IFSC code\n";
   }
   return 0;
}

Output

Valid IFSC code

Method 2: Check the first four characters

The first four characters of IFSC Code identify the bank. One can use a regular expression to check that the first four characters are alphabets.

^[A-Z]{4} 

这个正则表达式匹配任意四个大写字母。

语法

这是一个用于检查IFSC代码前四个字符的正则表达式 -

^([A-Z]{4})

这个正则表达式使用以下语法 -

  • ^  匹配字符串的开头。

  • [A-Z]  匹配任何大写字母。

  • {4}  指定前面的模式应该恰好出现四次。

  • ()  Creates a capture group to extract the matched text.

这个正则表达式将匹配任何以四个大写字母开头的字符串。要验证整个IFSC代码,需要检查除了前四个字符之外的其他条件。

算法

Here is a step-by-step algorithm for validating the first four characters of an IFSC code using a regular expression −

步骤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中文网其他相关文章!

声明
本文转载于:tutorialspoint。如有侵权,请联系admin@php.cn删除
C#vs. C:内存管理和垃圾收集C#vs. C:内存管理和垃圾收集Apr 15, 2025 am 12:16 AM

C#使用自动垃圾回收机制,而C 采用手动内存管理。1.C#的垃圾回收器自动管理内存,减少内存泄漏风险,但可能导致性能下降。2.C 提供灵活的内存控制,适合需要精细管理的应用,但需谨慎处理以避免内存泄漏。

超越炒作:评估当今C的相关性超越炒作:评估当今C的相关性Apr 14, 2025 am 12:01 AM

C 在现代编程中仍然具有重要相关性。1)高性能和硬件直接操作能力使其在游戏开发、嵌入式系统和高性能计算等领域占据首选地位。2)丰富的编程范式和现代特性如智能指针和模板编程增强了其灵活性和效率,尽管学习曲线陡峭,但其强大功能使其在今天的编程生态中依然重要。

C社区:资源,支持和发展C社区:资源,支持和发展Apr 13, 2025 am 12:01 AM

C 学习者和开发者可以从StackOverflow、Reddit的r/cpp社区、Coursera和edX的课程、GitHub上的开源项目、专业咨询服务以及CppCon等会议中获得资源和支持。1.StackOverflow提供技术问题的解答;2.Reddit的r/cpp社区分享最新资讯;3.Coursera和edX提供正式的C 课程;4.GitHub上的开源项目如LLVM和Boost提升技能;5.专业咨询服务如JetBrains和Perforce提供技术支持;6.CppCon等会议有助于职业

c#vs. c:每种语言都擅长c#vs. c:每种语言都擅长Apr 12, 2025 am 12:08 AM

C#适合需要高开发效率和跨平台支持的项目,而C 适用于需要高性能和底层控制的应用。1)C#简化开发,提供垃圾回收和丰富类库,适合企业级应用。2)C 允许直接内存操作,适用于游戏开发和高性能计算。

继续使用C:耐力的原因继续使用C:耐力的原因Apr 11, 2025 am 12:02 AM

C 持续使用的理由包括其高性能、广泛应用和不断演进的特性。1)高效性能:通过直接操作内存和硬件,C 在系统编程和高性能计算中表现出色。2)广泛应用:在游戏开发、嵌入式系统等领域大放异彩。3)不断演进:自1983年发布以来,C 持续增加新特性,保持其竞争力。

C和XML的未来:新兴趋势和技术C和XML的未来:新兴趋势和技术Apr 10, 2025 am 09:28 AM

C 和XML的未来发展趋势分别为:1)C 将通过C 20和C 23标准引入模块、概念和协程等新特性,提升编程效率和安全性;2)XML将继续在数据交换和配置文件中占据重要地位,但会面临JSON和YAML的挑战,并朝着更简洁和易解析的方向发展,如XMLSchema1.1和XPath3.1的改进。

现代C设计模式:构建可扩展和可维护的软件现代C设计模式:构建可扩展和可维护的软件Apr 09, 2025 am 12:06 AM

现代C 设计模式利用C 11及以后的新特性实现,帮助构建更灵活、高效的软件。1)使用lambda表达式和std::function简化观察者模式。2)通过移动语义和完美转发优化性能。3)智能指针确保类型安全和资源管理。

C多线程和并发:掌握并行编程C多线程和并发:掌握并行编程Apr 08, 2025 am 12:10 AM

C 多线程和并发编程的核心概念包括线程的创建与管理、同步与互斥、条件变量、线程池、异步编程、常见错误与调试技巧以及性能优化与最佳实践。1)创建线程使用std::thread类,示例展示了如何创建并等待线程完成。2)同步与互斥使用std::mutex和std::lock_guard保护共享资源,避免数据竞争。3)条件变量通过std::condition_variable实现线程间的通信和同步。4)线程池示例展示了如何使用ThreadPool类并行处理任务,提高效率。5)异步编程使用std::as

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
4 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
4 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
4 周前By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解锁Myrise中的所有内容
1 个月前By尊渡假赌尊渡假赌尊渡假赌

热工具

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

安全考试浏览器

安全考试浏览器

Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

螳螂BT

螳螂BT

Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

WebStorm Mac版

WebStorm Mac版

好用的JavaScript开发工具