Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Regex: match only chars which aren't the same in a string

Got a question regarding regex matching in Java.
Given is a string with a defined length.
Now I want to check with a matcher if every char contained in that String is different.

For example (only pass of length = 8):

String a = "abcdefgz" -> pass
String b = "aacdefgz" -> fail
String c = "abcdefghz" -> fail

So matching the length would simply be:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

"^[a-zA-Z]{8}$"

But to get it working in combination with the condition that every contained char needs to be unique is quite tough.

>Solution :

Well, this one matches strings containing duplicates. This is basically the reverse of what you are looking for.

.*(.)+.*\1.*

If it’s suitable, you can pick string that do not match this expression. Or maybe check regex negative match to improve it.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading