1

I'm validating the Edittext with regex by allowing particular characters in that i need to allow all the special characters to enter in edit text.

for allowing alpha and numbers im using the code

edittext.setFilters(new InputFilter[] { new PartialRegexInputFilter(
                    "[a-zA-z0-9]+") });

Like this i need to allow all special characters...

And also i try to give like this

edittext.setFilters(
new InputFilter[] 
    { new PartialRegexInputFilter(
        "[A-Za-z0-9!#$%&(){|}~:;<=>?@*+,./^_`-\'\" \t\r\n\f]+") 
    }
);` . 

But this give error for single and double quote characters...

4

3 回答 3

2

你有什么要禁止的吗?听起来您正在尝试允许所有字母数字字符允许所有非字母数字(即特殊)字符。

以下正则表达式将匹配所有特殊字符:

[^A-Za-z0-9]
于 2012-12-26T15:02:47.737 回答
1

不要添加每个特殊字符,而是使用.*. 此正则表达式匹配所有内容。

如果要从中删除某些字符,请[^a!c]单独使用,.*使用, 来匹配除a,!和之外的所有内容c

于 2018-07-10T20:43:09.253 回答
1

我认为您的第二种模式的问题是字符(-)集中间的连字符。把它移到最后:

[A-Za-z0-9!#$%&(){|}~:;<=>?@*+,./^_`\'\" \t\r\n\f-]+

连字符不能在中间,因为它们指定字符范围 [az] 指定 a 到 z 之间的所有小写字母。

于 2018-07-10T20:11:25.707 回答