📌  相关文章
📜  如何使用正则表达式检查字符串是否为字母数字

📅  最后修改于: 2021-09-06 05:33:22             🧑  作者: Mango

给定字符串str ,任务是使用正则表达式检查字符串是否为字母数字。

例子:

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

  • 获取字符串。
  • 创建一个正则表达式来检查字符串是否为字母数字,如下所述:
regex = "^(?=.*[a-zA-Z])(?=.*[0-9])[A-Za-z0-9]+$";
  • 在哪里:
    • ^代表字符串的开始
    • (?=.*[a-zA-Z])表示从 az, AZ 开始的字母
    • (?=.*[0-9])表示 0-9 之间的任何数字
    • [A-Za-z0-9]代表一切是字母还是数字
    • +代表一次或多次
    • $代表字符串的结尾
  • 将给定的字符串与正则表达式匹配,在Java,这可以通过使用 Pattern.matcher() 来完成
  • 如果字符串与给定的正则表达式匹配,则返回 true,否则返回 false

下面是上述方法的实现。

Java
// Java program to check string is
// alphanumeric or not using Regular Expression.
 
import java.util.regex.*;
 
class GFG {
 
    // Function to check string is alphanumeric or not
    public static boolean isAlphaNumeric(String str)
    {
        // Regex to check string is alphanumeric or not.
        String regex = "^(?=.*[a-zA-Z])(?=.*[0-9])[A-Za-z0-9]+$";
 
        // 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 given string
        // and 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 = "GeeksforGeeks123";
        System.out.println(
            str1 + ": "
            + isAlphaNumeric(str1));
 
        // Test Case 2:
        String str2 = "GeeksforGeeks";
        System.out.println(
            str2 + ": "
            + isAlphaNumeric(str2));
 
        // Test Case 3:
        String str3 = "GeeksforGeeks123@#";
        System.out.println(
            str3 + ": "
            + isAlphaNumeric(str3));
 
        // Test Case 4:
        String str4 = "123";
        System.out.println(
            str4 + ": "
            + isAlphaNumeric(str4));
 
        // Test Case 5:
        String str5 = "@#";
        System.out.println(
            str5 + ": "
            + isAlphaNumeric(str5));
    }
}


Python3
# Python3 program to check
# string is alphanumeric or
# not using Regular Expression.
import re
 
# Function to check string
# is alphanumeric or not
def isAlphaNumeric(str):
 
    # Regex to check string is
    # alphanumeric or not.
    regex = "^(?=.*[a-zA-Z])(?=.*[0-9])[A-Za-z0-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 = "GeeksforGeeks123"
print(str1, ":",
      isAlphaNumeric(str1))
 
# Test Case 2:
str2 = "GeeksforGeeks"
print(str2, ":",
      isAlphaNumeric(str2))
 
# Test Case 3:
str3 = "GeeksforGeeks123@#"
print(str3, ":",
      isAlphaNumeric(str3))
 
# Test Case 4:
str4 = "123"
print(str4, ":",
      isAlphaNumeric(str4))
 
# Test Case 5:
str5 = "@#"
print(str5, ":",
      isAlphaNumeric(str5))
 
# This code is contributed by avanitrachhadiya2155


输出:
GeeksforGeeks123: true
GeeksforGeeks: false
GeeksforGeeks123@#: false
123: false
@#: false


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