简体   繁体   中英

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] .

I found out that Pattern.match() only checks whether the whole string matches the pattern. So I tried this pattern: .*[^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.

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]

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() .

try "(?s).*[^AZ\\\\s].*" it turns on dotall mode. In dotall mode, the expression . matches any character, including a line terminator. By default this expression does not match line terminators. See Pattern.API for (?idmsuxU-idmsuxU)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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