模式不允许包含数字的单词?

DJ NoReality TXC_TryhardOne:

我编写了一个程序,可以添加类别,因为在第一位出现了一些特殊字符和数字的问题,因此我制作了一个regex过滤器,该过滤器只应处理特殊字符。但是,如果我现在使用包含数字的单词,则由于某种原因该方法也会返回true。

private boolean containsSpecChar () {
  Pattern pattern = Pattern.compile("[a-zA-Z0-9]");
  Pattern p = Pattern.compile("[0-9a-zA-Z]");
  String a = null;
  a = txtInKategorieName.getText();
  Matcher match= pattern.matcher(a);
  Matcher m = p.matcher(a);
  if (
  match.matches() || m.matches()
  )
  {
    return false;
  }
  else
  {
    return true;
  }
}

我希望也可以使用包含数字的单词。谢谢

安德烈亚斯(Andreas):

[a-zA-Z0-9]并且[0-9a-zA-Z]是同一回事。

[xxx]正则表达式模式是一个字符类,它相匹配的单个字符。如果要匹配这些字符中的一个或多个,则需要在末尾添加一个+ 量词

"[a-zA-Z0-9]+"

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章