简体   繁体   English

包含至少 4 个唯一可打印 ASCII 字符(不包括少数特殊字符)的字符串的正则表达式

[英]Regex for strings containing at least 4 unique printable ASCII characters excluding few special characters

I am working on an application, in which input strings must have following properties:我正在开发一个应用程序,其中输入字符串必须具有以下属性:

  • Contains at least 4 unique characters包含至少 4 个唯一字符
  • Doesn't contain non-printable ASCII characters不包含不可打印的 ASCII 字符
  • Doesn't contain comma (,), colon (:), equals sign (=), or space不包含逗号 (,)、冒号 (:)、等号 (=) 或空格

I came up with below regex, which matches strings with at least 4 unique chars, and all printable ASCII chars excluding space.我想出了下面的正则表达式,它匹配至少有 4 个唯一字符的字符串,以及所有可打印的 ASCII 字符,不包括空格。

^([!-~]+)((?!\1)[!-~]+)((?!\1|\2)[!-~]+)((?!\1|\2|\3)[!-~]+)$

How do I modify it to exclude comma (,), colon (:), equals sign (=)?如何修改它以排除逗号 (,)、冒号 (:)、等号 (=)?

In Java you can make use of Character Class Subtraction .在 Java 中,您可以使用Character Class Subtraction

The range can look like [!-~&&[^,:=]]范围看起来像[!-~&&[^,:=]]

^([!-~&&[^,:=]]+)((?!\1)[!-~&&[^,:=]]+)((?!\1|\2)[!-~&&[^,:=]]+)((?!\1|\2|\3)[!-~&&[^,:=]]+)$

In Java:在 Java 中:

String regex = "^([!-~&&[^,:=]]+)((?!\\1)[!-~&&[^,:=]]+)((?!\\1|\\2)[!-~&&[^,:=]]+)((?!\\1|\\2|\\3)[!-~&&[^,:=]]+)$";

Regex demo正则表达式演示

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

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