Green goblin
Green goblin

Reputation: 9996

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:

I came up with below regex, which matches strings with at least 4 unique chars, and all printable ASCII chars excluding space.

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

How do I modify it to exclude comma (,), colon (:), equals sign (=)?

Upvotes: 0

Views: 78

Answers (1)

The fourth bird
The fourth bird

Reputation: 163517

In Java you can make use of Character Class Subtraction.

The range can look like [!-~&&[^,:=]]

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

In Java:

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

Regex demo

Upvotes: 2

Related Questions