使用正则表达式匹配除=以外的任何字符 [英] Using regex to match any character except =
问题描述
我正在尝试编写一个String验证来匹配任何字符(常规,数字和特殊),除了=。
I am trying to write a String validation to match any character (regular, digit and special) except =.
这是我写的 -
String patternString = "[[^=][\\w\\s\\W]]*";
Pattern p = Pattern.compile(patternString);
Matcher m = p.matcher(str);
if(m.matches())
System.out.println("matches");
else
System.out.println("does not");
但是,它与输入字符串2009-09 / 09 12:23:12.5 =匹配模式。
But, it matches the input string "2009-09/09 12:23:12.5=" with the pattern.
如何从模式字符串中排除=(或任何其他字符)?
How can I exclude = (or any other character, for that matter) from the pattern string?
推荐答案
如果唯一禁止的字符是等号,那么 [^ =] *
之类的东西应该有用。
If the only prohibited character is the equals sign, something like [^=]*
should work.
[^ ...]
是一个否定的字符类;它匹配单个字符,除了方括号中的列表之外的任何字符。 *
重复表达式零次或多次。
[^...]
is a negated character class; it matches a single character which is any character except one from the list between the square brackets. *
repeats the expression zero or more times.
这篇关于使用正则表达式匹配除=以外的任何字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!