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

📅  最后修改于: 2021-04-29 07:12:48             🧑  作者: 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,使用regex匹配给定的字符串,这可以通过使用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