📌  相关文章
📜  如何使用正则表达式验证印度的Pin码

📅  最后修改于: 2021-04-23 19:03:22             🧑  作者: Mango

给定一个介于0到9之间的正数字符串,任务是使用正则表达式检查该数字是否为有效的密码

例子:

方法:可以通过使用正则表达式来解决此问题。

  1. 获取号码。
  2. 创建一个正则表达式以验证印度的PIN码,如下所述:
    regex = "^[1-9]{1}[0-9]{2}\\s{0, 1}[0-9]{3}$";
    

    在哪里:

    • ^代表数字的开头。
    • [1-9] {1}表示PIN码中的起始数字,范围从1到9。
    • [0-9] {2}表示密码中的后两位数字,范围从0到9。
    • \\ s {0,1}表示引脚代码中的空白,该空白可以出现一次或永远不会发生。
    • [0-9] {3}表示密码中的后三位数字,范围为0到9。
    • $代表数字的结尾。
  3. 在Java,使用regex匹配给定的数字,这可以通过使用Pattern.matcher()来完成。
  4. 如果数字与给定的正则表达式匹配,则返回true,否则返回false。

下面是上述方法的实现。

Java
// Java program to validate the pin code
// of India using Regular Expression.
  
import java.util.regex.*;
  
class GFG {
  
    // Function to validate the pin code of India.
    public static boolean isValidPinCode(String pinCode)
    {
  
        // Regex to check valid pin code of India.
        String regex
            = "^[1-9]{1}[0-9]{2}\\s{0,1}[0-9]{3}$";
  
        // Compile the ReGex
        Pattern p = Pattern.compile(regex);
  
        // If the pin code is empty
        // return false
        if (pinCode == null) {
            return false;
        }
  
        // Pattern class contains matcher() method
        // to find matching between given pin code
        // and regular expression.
        Matcher m = p.matcher(pinCode);
  
        // Return if the pin code
        // matched the ReGex
        return m.matches();
    }
  
    // Driver Code.
    public static void main(String args[])
    {
  
        // Test Case 1:
        String num1 = "132103";
        System.out.println(
            num1 + ": "
            + isValidPinCode(num1));
  
        // Test Case 2:
        String num2 = "201 305";
        System.out.println(
            num2 + ": "
            + isValidPinCode(num2));
  
        // Test Case 3:
        String num3 = "014205";
        System.out.println(
            num3 + ": "
            + isValidPinCode(num3));
  
        // Test Case 4:
        String num4 = "1473598";
        System.out.println(
            num4 + ": "
            + isValidPinCode(num4));
    }
}


Python3
# Python3 program to validate the  
# pin code of India using Regular 
# Expression. 
import re
  
# Function to validate the pin code
# of India. 
def isValidPinCode(pinCode):
      
    # Regex to check valid pin code
    # of India.
    regex = "^[1-9]{1}[0-9]{2}\\s{0,1}[0-9]{3}$"; 
  
    # Compile the ReGex 
    p = re.compile(regex);
      
    # If the pin code is empty
    # return false
    if (pinCode == ''):
        return False;
          
    # Pattern class contains matcher() method
    # to find matching between given pin code
    # and regular expression.
    m = re.match(p, pinCode);
      
    # Return True if the pin code
    # matched the ReGex else False
    if m is None:
        return False
    else:
        return True
  
# Driver code 
if __name__ == "__main__":
      
    # Test case 1
    num1 = "132103";
    print(num1, ": ", isValidPinCode(num1));
      
    # Test case 2:
    num2 = "201 305";
    print(num2, ": ", isValidPinCode(num2));
      
    # Test case 3:
    num3 = "014205";
    print(num3, ": ", isValidPinCode(num3));
      
    # Test case 4:
    num4 = "1473598";
    print(num4, ": ", isValidPinCode(num4));
      
# This code is contributed by AnkitRai01


输出:
132103: true
201 305: true
014205: false
1473598: false