Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular Expression - Exclude list of words for a name

I'm trying to make a regular expression that accepts this:

  • Only a-z, 0-9, _ chars, with a minimum length of 3
  • admin, static, my and www are rejected.

For the first part, I already managed to do it with :

^[a-zA-Z0-9\\_]{3,}$

But I don't know how to exclude the words listed previously.

For example, that would mean :

  • static is not allowed (of course), but
  • statice is allowed
  • estatic is allowed

Using this regular expression :

^(?!static|my|admin|www).*$

doesn't work well : it excludes statice (and everything after the unauthorized word).

Do you know which regular expression will fit my need?

like image 907
Cyril N. Avatar asked Apr 04 '11 15:04

Cyril N.


1 Answers

Try something like this:

^(?!static$|my$|admin$|www$).*$

This will disallow "static" but allow "statice", "statica", etc. By anchoring each blacklisted word to the end of the string you will only match them if they are standing alone without any trailing characters.

Edit: codeaddict has suggested a cleaner way to do basically the same thing:

^(?!(?:static|my|admin|www)$).*$

like image 132
Andrew Hare Avatar answered Nov 05 '22 02:11

Andrew Hare