Agum Posted May 2, 2007 Share Posted May 2, 2007 I need a RegEx pattern that checks for usernames. The username must consist of only alphanumeric characters or hyphen (-) or underscore (_). The thing is, hyphen or underscore must have an alphanumeric character before and after it. Test cases: Spiderman = ok Spider-man = ok Spider_man = ok Spider--man = err Spider__man = err -Spiderman = err Spiderman_ = err Spider-man-2 = ok 1_Spider-man = ok me and my friend came up with two different ways to do this: ^[a-zA-Z0-9]+([-_][a-zA-Z0-9]+)*$ matches the "good" stuff (return true on OK, return false on ERR) ^[-_]|[^a-zA-Z0-9-_]|(--)|(-_)|(_-)|(__)|[-_]$ matches the "bad" stuff (return true on ERR, return false on OK) which one would be more efficient? if they are both pretty bad, do you have a suggestion for a better way to do this? also, what's the difference between preg and ereg in php? which one do you recommend and why? Quote Link to comment Share on other sites More sharing options...
monk.e.boy Posted May 2, 2007 Share Posted May 2, 2007 why is 'spider--man' and error? Why not forget regex and just look for illegal characters, if '-' is illegal, then trow out 'spider-man' if '-' is legal then 'po---------ooo' is legal. is 'jooooooooohn' legal? how about 'jo------oooooo----n'? I think your logic is a bit wonky. monk.e.boy Quote Link to comment Share on other sites More sharing options...
Wildbug Posted May 2, 2007 Share Posted May 2, 2007 Since your undesireable characteristics are so well defined, I would go with the regex that matches what you DON'T want. No beginning hyphen/underscore, no double hyphen/underscore, no non-word/non-hyphen characters, no ending hyphen/underscore. <?php $usernames = array( 'Spiderman', 'Spider-man', 'Spider_man', 'Spider--man', 'Spider__man', '-Spiderman', 'Spiderman_', 'Spider-man-2', '1_Spider-man', 'Spider & Man' ); foreach ($usernames as $uname) echo $uname . (preg_match('/^[-_]|[-_]{2,}|[^\w-]|[-_]$/',$uname) ? " fails\n" : " is acceptable\n"); ?> Spiderman is acceptable Spider-man is acceptable Spider_man is acceptable Spider--man fails Spider__man fails -Spiderman fails Spiderman_ fails Spider-man-2 is acceptable 1_Spider-man is acceptable Spider & Man fails I'm guessing you'll only be using this comparison when a new user is registering a new username, right? This will be plenty fast for that occasion. If you were creating a program that had to plow through thousands of names very often (i.e., performance was paramount), a more efficient way to do this would probably be to split up the conditions and string them together with ORs. That way as soon as a failure is encountered, execution moves on. But that's just academic, using the method above will work fine. As far as preg vs. ereg, I personally prefer the preg* functions if only because I learned regular expressions when learning Perl. I think there are some extensions to the Perl-style syntax that give them more power than the POSIX ones. I don't know about speed/efficiency, however. There are some comparisons and caveats in the manual for both of these. Quote Link to comment Share on other sites More sharing options...
Agum Posted May 2, 2007 Author Share Posted May 2, 2007 thanks Wildbug. that's a good point - since it only needs to run at account sign-up, I guess it's ok even if it's a bit unefficient. mon.e.boy, if you have played WoW, you will know that character names allow 1 hyphen, and it cannot be in the first or last char, and you cannot have 2 hyphens consecutively. my sytem has similar rules except a few differences, such as allowing underscore, etc. * how do I mark this thread as SOLVED and change the icon? Quote Link to comment Share on other sites More sharing options...
Wildbug Posted May 2, 2007 Share Posted May 2, 2007 I think the topic solved link is at the bottom of your thread. Quote Link to comment Share on other sites More sharing options...
satant Posted May 2, 2007 Share Posted May 2, 2007 why u make all of that i think u only need to writ the unacceaptaple leteers Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.