如何使用正则表达式检查字符串是否为字母数字?

2021年4月15日19:00:29 发表评论 791 次浏览

本文概述

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

字母数字字符串是仅包含a-z, A-Z和0-9的数字的字符串。

例子:

输入:str =" lsbin123"
输出:true
说明:该字符串包含a-z, A-Z中的所有字母以及0-9中的数字。因此, 它是一个字母数字字符串。
输入:str =" lsbin"
输出:false
说明:该字符串包含a-z, A-Z中的所有字母, 但不包含0-9之间的任何数字。因此, 它不是字母数字字符串。
输入:str =" lsbin123 @#"
输出:false
说明:该字符串包含a-z, A-Z中的所有字母以及0-9中的数字以及一些特殊符号。因此, 它不是字母数字字符串。

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

regex = "^(?=.*[a-zA-Z])(?=.*[0-9])[A-Za-z0-9]+$";
  • 其中:
    • ^代表字符串的开头
    • (?=.* [a-zA-Z])代表a-z, A-Z中的字母
    • (?=.* [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 = "lsbin123" ;
         System.out.println(
             str1 + ": "
             + isAlphaNumeric(str1));
 
         //Test Case 2:
         String str2 = "lsbin" ;
         System.out.println(
             str2 + ": "
             + isAlphaNumeric(str2));
 
         //Test Case 3:
         String str3 = "lsbin123@#" ;
         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 = "lsbin123"
print (str1, ":" , isAlphaNumeric(str1))
 
# Test Case 2:
str2 = "lsbin"
print (str2, ":" , isAlphaNumeric(str2))
 
# Test Case 3:
str3 = "lsbin123@#"
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

输出如下:

lsbin123: true
lsbin: false
lsbin123@#: false
123: false
@#: false

木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: