Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expression to allow a set of characters and disallow others

Tags:

java

regex

I want to restrict the users from entering the below special characters in a field:

œçşÇŞ
ğĞščřŠŘŇĚŽĎŤČňěž
ůŮ
İťı
—¿„”*@
Newline
Carriage return

A few more will be added to this list but I will have the complete restricted list eventually.

But he can enter certain foreign characters like äöüÄÖÜÿï etc in addition to alphanumeric chars, usual special chars etc.

Is there an easy way to build a regex for doing this. Adding so many chars in the not allowed list like

[^œçşÇŞ ğĞščřŠŘŇĚŽĎŤČňěž ůŮ İ ť ı — ¿ „ ” * @]+

does not seem to work.

And I do not have the complete list of allowed characters. It would be too long even if I try to get it and would include all chars like:

~`!#$%^&()[]{};':",.

along with certain foreign chars.


1 Answers

You do not mention what "flavor" of regex you are using. Does the following work?

\A[^œçşÇŞ ğĞščřŠŘŇĚŽĎŤČňěž ůŮ İ ť ı — ¿ „ ” * @]+\z
like image 120
Lieven Keersmaekers Avatar answered Sep 07 '25 19:09

Lieven Keersmaekers