简体   繁体   English

搜索正则表达式模式以查找“禁止”字符

[英]Searching a regex pattern to find “forbidden” characters

I've googled a lot for it, but I cannot find any solution. 我为此做了很多搜索,但是找不到任何解决方案。 For a school project I need to find unsupported chars unsupported chars in a string. 对于学校项目,我需要在字符串中查找不受支持的字符。 Allowed is [AZ\\s] . 允许为[AZ\\s]

I found out that Pattern.match() only checks whether the whole string matches the pattern. 我发现Pattern.match()仅检查整个字符串是否与模式匹配。 So I tried this pattern: .*[^AZ\\\\s].* 所以我尝试了这种模式: .*[^AZ\\\\s].*

It works as long as you don't have any newline characters in the string. 只要字符串中没有换行符,它就可以工作。 To check them too, i've used [.\\\\s]*[^AZ\\\\s][.\\\\s]* to handle them as well, but now nothing works any more. 为了检查它们,我也使用[.\\\\s]*[^AZ\\\\s][.\\\\s]*来处理它们,但是现在没有任何作用了。

What would be the correct regex for this purpose? 为此目的正确的正则表达式是什么?

Either: 要么:

  • just invert the match, 只是反转比赛,
  • or invert the character class and try and find one character only: 或反转字符类并尝试仅找到一个字符:

[go around SO bug -- can't quote code right after a list item] [绕开SO错误-不能在列表项后立即引用代码]

final Pattern p = Pattern.compile("[^A-Z\\s]");
if (p.matcher(input).find())
    // illegal input, bark

Yes, .matches() is misnamed... Real regex matching in Java is done using .find() . 是的, .matches()的名称错误... Java中真正的正则表达式匹配是使用.find()

try "(?s).*[^AZ\\\\s].*" it turns on dotall mode. 尝试"(?s).*[^AZ\\\\s].*"会启用dotall模式。 In dotall mode, the expression . 在dotall模式下,表达式. matches any character, including a line terminator. 匹配任何字符,包括行终止符。 By default this expression does not match line terminators. 默认情况下,此表达式不匹配行终止符。 See Pattern.API for (?idmsuxU-idmsuxU) 请参阅Pattern.API以获取(?idmsuxU-idmsuxU)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM