简体   繁体   English

正则表达式允许使用Java中的特定特殊字符

[英]Regular Expression allows specific special characters in java

I need to know the regular expression for string that contains alphanumeric characters, @, underscore(_), full stop(.)and not any blank spaces. 我需要知道包含字母数字字符,@,下划线(_),句号(。)且没有任何空格的字符串的正则表达式。 And also for alphanumeric characters and it allow spaces. 并且也适用于字母数字字符,并且允许空格。 I tried with this regex, 我尝试过这个正则表达式,

^[_A-Za-z0-9-\\.\\@]$ and ^[A-Za-z0-9-\\s]$

CODE: 码:

private static final String Username_REGEX ="^[_A-Za-z0-9.@-]$";

public static boolean isUsername(EditText editText, boolean required) {
    return isValid(editText, Username_REGEX,Username_MSG, required);
}


public static boolean isValid(EditText editText, String regex, String errMsg, boolean required) {
    String text = editText.getText().toString().trim();

    editText.setError(null);

  if ( required && !hasTextemt(editText) ) return false;

    if (required && !Pattern.matches(regex, text)) {
        editText.setError(errMsg);
        return false;
    };

    return true;
}

public static boolean hasTextemt(EditText editText) {

    String text = editText.getText().toString().trim();
    editText.setError(null);


    if (text.length() == 0) {
        editText.setError(emt);
        return false;
    }

    return true;
}

Is this correct? 这个对吗? I did not get proper result. 我没有得到适当的结果。 Can anyone guide me? 谁能指导我?

Move the dash - at the end of the character class: 移动破折号-在字符类的末尾:

^[_A-Za-z0-9.@-]+$

and

^[A-Za-z0-9\\s-]+$

Between two characters it means a range. 在两个字符之间表示一个范围。

Edit: You also need a + modifier to match one or more of the characters in the character class. 编辑:您还需要一个+修饰符来匹配字符类中的一个或多个字符。

I am assuming that you are getting this input via an EditText widget. 我假设您正在通过EditText小部件获得此输入。 So inside the layout of the XML file you can add the following properties by which it will receive only specified characters. 因此,可以在XML文件的布局内添加以下属性,以使其仅接收指定的字符。 :

android:digits="abcdefghijklmnopqrstuvwxyz0123456789,.-@_"

note that it wont allow any capital letter. 请注意,它将不允许使用任何大写字母。

just add any digits/keys you want your user to be able to enter. 只需添加您希望用户能够输入的任何数字/键即可。 If you are not worried about the patterns and number of occurrence of any character then you don't even need any regex . 如果您担心任何字符的模式出现次数 ,那么您甚至不需要任何正则表达式

Hope it helps 希望能帮助到你

Try 尝试

"[\\w@\\.]+" //for alphanumeric, @, .

"[\\w\\s]+" //for alphanumeric, spaces

Add ^ and $ if you need that matches the whole word. 如果需要匹配整个单词,请添加^和$。

PS: For testing regexp I always use RegexPlanet (not spam :P) PS:对于测试regexp,我始终使用RegexPlanet (而不是垃圾邮件:P)

Hope it helps. 希望能帮助到你。

You are only missing a quantifier . 您只缺少一个量词 In your expression ^[_A-Za-z0-9.@-]$ , the character class [_A-Za-z0-9.@-] matches exactly one character out of the class. 在表达式^[_A-Za-z0-9.@-]$字符类 [_A-Za-z0-9.@-]与该类中的一个字符完全匹配。 To allow repeated characters, you need to define a quantifier . 要允许重复的字符,您需要定义一个数量词

* short for {0,} matches 0 or more characters (==> this allows the empty string!) * {0,}缩写{0,}匹配0个或多个字符(==>这允许空字符串!)

+ short for {1,} matches 1 or more characters + {1,}缩写{1,}匹配1个或更多字符

{n,m} matches minimum n and maximum m characters. {n,m}匹配最小n字符和最大m字符。

So your regex would look like 所以你的正则表达式看起来像

^[_A-Za-z0-9.@-]+$

if you require 1 or more characters, or 如果您需要1个或多个字符,或者

^[_A-Za-z0-9.@-]{6,20}$

if you want at least 6 characters and at most 20. 如果您想要至少6个字符且最多20个字符。

Other things: 其他事情:

  • You can replace _A-Za-z0-9 by \\w , but be aware, \\w is Unicode based and contains all letters and digits from all languages. 您可以用\\w替换_A-Za-z0-9 ,但是请注意, \\w是基于Unicode的,并且包含所有语言的所有字母和数字。

  • A-Za-z is only ASCII, maybe you want to have a look at Unicode properties . A-Za-z仅是ASCII,也许您想看看Unicode属性 With eg \\p{L} you can match a letter of any language. 使用\\p{L}可以匹配任何语言的字母。

You're missing a plus sign (meaning one or more) at the end of the character class, and you can simplify considerably: 在字符类的末尾缺少加号(意味着一个或多个), 并且可以大大简化:

^[\\w.@]+$

Characters within a character class lose their special meanings so don't need to be escaped, except for square brackets and a couple of others. 字符类中的字符失去了特殊的含义,因此除了方括号和其他几个字符外,不需要转义。


For alphanumeric and spaces only, that is only combinations of letters, numbers and spaces: 仅对于字母数字和空格,即字母,数字和空格的组合:

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

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

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