[英]Using regex to match any character except =
I am trying to write a String validation to match any character (regular, digit and special) except =. 我正在尝试编写一个字符串验证来匹配除=以外的任何字符(常规,数字和特殊)。
Here is what I have written - 这是我写的 -
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");
But, it matches the input string "2009-09/09 12:23:12.5=" with the pattern. 但是,它与输入字符串“2009-09 / 09 12:23:12.5 =”匹配模式。
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. *
重复表达式零次或多次。
如果您只想检查是否出现“=”,为什么不使用String indexOf()方法?
if str.indexOf('=') //...
如果您的目标是在字符串中没有任何=字符,请尝试以下操作
String patternString = "[^=]*";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.