aim25 Posted July 14, 2008 Share Posted July 14, 2008 Quick yes or no: Is this right for validating a string (between 5 to 15 characters) which may consist of A-Z (both capital and lower case), 0-9, hyphen(-), and underscore(_)? /[^a-zA-Z0-9_\-]/ Thank you for the help. Quote Link to comment https://forums.phpfreaks.com/topic/114729-preg_match/ Share on other sites More sharing options...
rhodesa Posted July 14, 2008 Share Posted July 14, 2008 no, that will find 1 character that is anything BUT what you have... from left to right... / => starts the regex [ => starts a character group ^ => declares that the match should be anything except for what is in the character group a-zA-Z0-9_\- => matches as you described ] => ends character group / => ends regex also, you don't specify to search from beginning to send. so as long as there is ONE character that is NOT one of the ones you described, it will match. theoretically, you could use this, and a TRUE would mean it's not what you want, but let's modify the regex instead: /^[\w\-]{5,15}$/ / => starts the regex ^ => forces the match to start at the beginning of the string [ => starts character group \w => matches a-z, A-Z, 0-9, and underscore (much easier then writing all those out) \- => matches a - (i think you can leave out the \ before it cus it's in a character group, but not 100% sure) ] => end character group {5,15} => the previous character (or in our case character group) needs to repeat between 5 and 15 times $ => forces the match to go to the end of the string / => ends character group Quote Link to comment https://forums.phpfreaks.com/topic/114729-preg_match/#findComment-590024 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.