我正在尝试编写一个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 = Patternpile(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.
更多推荐
使用正则表达式匹配除=以外的任何字符
发布评论