有人可以帮助我使用正则表达式吗?只有当字符串包含字母或带空格的字母时,该表达式才返回 true.. 字符串可以包含数字或字母或带空格的字母。
这是我尝试过的
/^[ a-zA-Z0-9]+$
最佳答案
您可以尝试以下正则表达式:
[\p{Alnum} ]+
正则表达式说明:
NODE EXPLANATION
--------------- ------------------------------------------
[\p{Alnum} ]+ any character of: letters and digits, ' '
(1 or more times (matching the most amount
possible))
此外,如果您打算经常使用正则表达式,建议使用常量,以避免每次都重新编译,例如:
private static final Pattern REGEX_PATTERN =
Pattern.compile("[\\p{Alnum} ]+");
public static void main(String[] args) {
String input = ...
System.out.println(
REGEX_PATTERN.matcher(input).matches()
);
}
关于java - 匹配字母、数字或空格的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31870955/