简体   繁体   English

正则表达式以匹配字母或数字或空格

[英]regular expression to match alphabets or numeric or spaces

can somebody help me with a regular expression whichreturns true only if the string has alphabets or alphabet with spaces.. the string can have numbers or alphabets or alphabets with spaces. 有人可以用正则表达式为我提供帮助吗,该正则表达式仅在字符串包含字母或带空格的字母时才返回true。字符串可以包含数字或字母或带空格的字母。

this is what i have tried 这就是我尝试过的

/^[ a-zA-Z0-9]+$

You can try the following regular expression: 您可以尝试以下正则表达式:

[\p{Alnum} ]+

Explanation of the regular expression: 正则表达式的说明:

NODE             EXPLANATION
---------------  ------------------------------------------
[\p{Alnum} ]+    any character of: letters and digits, ' '
                 (1 or more times (matching the most amount
                 possible))

Additionally, if you plan to use the regular expression very often, it is recommended to use a constant in order to avoid recompile it each time, eg: 另外,如果您打算非常频繁地使用正则表达式,建议使用常量,以避免每次都重新编译它,例如:

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()
    );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM