Jack hardcastle
Jack hardcastle

Reputation: 2875

Regular expression for filtering similar characters

I'm trying to construct a regular expression to implement the following restriction:

The value may be of any length, but may not contain similar letters/numbers which may cause confusion when printed. E.g., 1 (the numeric), I (an upper-case Latin i) and l ( a lower case Latin L) look similar; there may be other such examples

I've tried a few things but can't get it to work, any help would be much appreciated: thanks.

For example: I could have Linux1 but not LINUX1 or linux1

Upvotes: 0

Views: 44

Answers (1)

npinti
npinti

Reputation: 52185

I doubt that regular expressions are the way to go here, but if you really need to, you would probably need to handle them on a case by case basis: (1I)|(I1)|([Ii][Ll])|([Ll][Ii]). So if the regex will match, you will have to reject the string. A working example of the regular expression can be found here.

As per your comments, you could try to use something like so: (1.*I)|(I.*1)|([Ii].*[Ll])|([Ll].*[Ii]). A working regex can be found here. This regular expression will also check between values which are deemed similar.

Upvotes: 3

Related Questions