limitphp Posted December 9, 2008 Share Posted December 9, 2008 Ok, I read through the tutorial that explained what they were and went over alot of the rules. It was an awesome tutorial and explained quite a bit, alot of it was straight forward and easy to understand. The braces will take a little time for me and the multiple line stuff will probably take some time for me. But I still can't figure out how to do what I want. For usernames, I would like the rules to be: 1. has to start with a letter [a-zA-Z] 2. has to end with a letter or number [a-zA-Z0-9] 3. It can contain letters, numbers, a dash, period, or underscore [a-zA-Z0-9._-] 4. a dash, period, or underscore has to be preceded and followed by a letter or number. In other words cannot have two dashes together, two periods together, or a dash and a period. I know this might be child's play for some of you, but it is pretty darn difficult for me, and any help will be appreciated.... I'll take a shot at it: /[a-zA-Z][a-zA-Z0-9(._-){1,}][a-zA-Z0-9]/ When I use it in my code to make sure the username meets all these criteria, do I do it like this: if(!preg_match("/[a-zA-Z][a-zA-Z0-9(._-){1,}][a-zA-Z0-9]/", $username)) return FALSE; Quote Link to comment https://forums.phpfreaks.com/topic/136261-need-help-with-a-regex/ Share on other sites More sharing options...
DarkWater Posted December 9, 2008 Share Posted December 9, 2008 Number 4 might be really tough actually. The rest is easy though: ^[a-zA-Z][\w.-]+[a-zA-Z\d]$ Notice the + instead of {1,}. Quote Link to comment https://forums.phpfreaks.com/topic/136261-need-help-with-a-regex/#findComment-710803 Share on other sites More sharing options...
limitphp Posted December 9, 2008 Author Share Posted December 9, 2008 Number 4 might be really tough actually. The rest is easy though: ^[a-zA-Z][\w._-]+[a-zA-Z\d]$ Notice the + instead of {1,}. if you use the \w then you don't need to have an underscore follow it, right, because the \w includes the underscore, so this instead? ^[a-zA-Z][\w.-]+[a-zA-Z\d]$ Quote Link to comment https://forums.phpfreaks.com/topic/136261-need-help-with-a-regex/#findComment-710809 Share on other sites More sharing options...
DarkWater Posted December 9, 2008 Share Posted December 9, 2008 Looks good. It's funny because I was thinking to myself "I should use \w so I don't need to put an underscore or anything", and I end up putting an underscore in there. Good catch. It's ironic that I wrote that tutorial and just made a mistake like that. =P Quote Link to comment https://forums.phpfreaks.com/topic/136261-need-help-with-a-regex/#findComment-710813 Share on other sites More sharing options...
limitphp Posted December 9, 2008 Author Share Posted December 9, 2008 Number 4 might be really tough actually. The rest is easy though: ^[a-zA-Z][\w._-]+[a-zA-Z\d]$ Notice the + instead of {1,}. also, the + would mean that something would have to come after the first part [a-zA-Z]? So, after they type a letter, the + means, they would have to have 1 character of a letter, number, underscore, period or digit following it, right? Which is fine, I'll make a minimum length it has to be. LIke at least 4 characters long. Quote Link to comment https://forums.phpfreaks.com/topic/136261-need-help-with-a-regex/#findComment-710815 Share on other sites More sharing options...
DarkWater Posted December 9, 2008 Share Posted December 9, 2008 You could use * instead of + if you wanted to allow things like a9 or something. Quote Link to comment https://forums.phpfreaks.com/topic/136261-need-help-with-a-regex/#findComment-710816 Share on other sites More sharing options...
limitphp Posted December 9, 2008 Author Share Posted December 9, 2008 Looks good. It's funny because I was thinking to myself "I should use \w so I don't need to put an underscore or anything", and I end up putting an underscore in there. Good catch. It's ironic that I wrote that tutorial and just made a mistake like that. =P hehehe.....although, since you wrote the tutorial in a way that was easy for newbies to understand, i was able to catch that mistake.... : ) So, what you have there will require at least 3 characters? Quote Link to comment https://forums.phpfreaks.com/topic/136261-need-help-with-a-regex/#findComment-710819 Share on other sites More sharing options...
limitphp Posted December 9, 2008 Author Share Posted December 9, 2008 You could use * instead of + if you wanted to allow things like a9 or something. No no, thats fine. I want their usernames to be at least 4 characters. I'll use a strlen to meet minimium length requirements. To keep the dashes, periods and underscore from repeating, could you group those together and put a maximum of 1? like ^[a-zA-Z][a-zA-Z\d(._-){0,1}]+[a-zA-Z\d]$ or something...I remember in the turorial you said you can't set a maxiumum only in the braces...maybe put a mimium of 0 and a maxiumum of 1. Quote Link to comment https://forums.phpfreaks.com/topic/136261-need-help-with-a-regex/#findComment-710827 Share on other sites More sharing options...
limitphp Posted December 9, 2008 Author Share Posted December 9, 2008 I'm good at understanding individual parts. Its applying it all together that takes alot of time for me. Like, can you put braces inside the ranges []. Quote Link to comment https://forums.phpfreaks.com/topic/136261-need-help-with-a-regex/#findComment-710830 Share on other sites More sharing options...
DarkWater Posted December 9, 2008 Share Posted December 9, 2008 You cannot have braces inside of a character class like that. It would just mean that the characters '{', '0', ',', '1', and '}' could appear in that spot. Your no-double-periods deal would probably require some really advanced stuff. In fact, I can't even think of an easy way to implement it in one nice regex. Quote Link to comment https://forums.phpfreaks.com/topic/136261-need-help-with-a-regex/#findComment-710839 Share on other sites More sharing options...
nrg_alpha Posted December 9, 2008 Share Posted December 9, 2008 Instead of all this [a-zA-Z] stuff, why just say [a-z] and then after your closing delimiter, simply add the i? Something minor (and thus may not be important at all), but \d encompasses more than 0-9 (think exponents: ² ³ etc..) so do you allow an user to create an username like Rage² ? If not, then explicitly stating [0-9] may be needed (I'm going to assume this is not an issue). So if I understand the rules correctly (minimum 4 characters [including ._ or -] must start with a letter, no consecutive ._ or - characters allowed.. and must end with either a letter or number.. this would be my take on it: // $str = 'A_1'; // Nope... too short (not 4 characters minimum // $str = 's.1_'; // Nope.. does not end with a letter or number // $str = 'r4.-67'; // Nope.. cannot have consecutive ._ or - characters $str = 't6-8U_98.1.2'; // yep.. meets all criteria if(preg_match('#^[a-z](?:[._-]?[a-z\d]){2,}$#i', $str)){ echo $str . ' yep'; } else { echo $str . ' nope'; } If I have misunderstood, then my apologies in advance Quote Link to comment https://forums.phpfreaks.com/topic/136261-need-help-with-a-regex/#findComment-710861 Share on other sites More sharing options...
nrg_alpha Posted December 9, 2008 Share Posted December 9, 2008 Oops.. one minor flaw is that if there is no ._ or - characters, it will allow 3 characters.. so just replace {2,} with + #^[a-z](?:[._-]?[a-z\d])+$#i And then as you mentioned before, check via strlen or something. Quote Link to comment https://forums.phpfreaks.com/topic/136261-need-help-with-a-regex/#findComment-710866 Share on other sites More sharing options...
DarkWater Posted December 10, 2008 Share Posted December 10, 2008 Ah, good idea. I was thinking of something like that; I just didn't have time to think about it. Nice. Quote Link to comment https://forums.phpfreaks.com/topic/136261-need-help-with-a-regex/#findComment-711050 Share on other sites More sharing options...
limitphp Posted December 10, 2008 Author Share Posted December 10, 2008 Instead of all this [a-zA-Z] stuff, why just say [a-z] and then after your closing delimiter, simply add the i? Something minor (and thus may not be important at all), but \d encompasses more than 0-9 (think exponents: ² ³ etc..) so do you allow an user to create an username like Rage² ? If not, then explicitly stating [0-9] may be needed (I'm going to assume this is not an issue). So if I understand the rules correctly (minimum 4 characters [including ._ or -] must start with a letter, no consecutive ._ or - characters allowed.. and must end with either a letter or number.. this would be my take on it: // $str = 'A_1'; // Nope... too short (not 4 characters minimum // $str = 's.1_'; // Nope.. does not end with a letter or number // $str = 'r4.-67'; // Nope.. cannot have consecutive ._ or - characters $str = 't6-8U_98.1.2'; // yep.. meets all criteria if(preg_match('#^[a-z](?:[._-]?[a-z\d]){2,}$#i', $str)){ echo $str . ' yep'; } else { echo $str . ' nope'; } If I have misunderstood, then my apologies in advance Yes, those are the correct rules. Thank you. Quick question, so I can try to understand this, what is the question mark and colon for at the begining of the grouping? You make a good point about the usernames being able to be pretty wierd with multiple dots and dashes, but my only alternative would be to only allow one: dash, period or underscore. I figure, if someone does a weird name like that, they'll have a really hard time remembering it. Also, no matter how many rules you have, there's almost always a way to make something strange and unusual within the rules. I'm going to go ahead and stay with these rules, otherwise I'm going to drive myself nuts coming up with them and it'll end up using up alot of my time coming up with them and then changing them constantly. Thanks again for yall's help. Quote Link to comment https://forums.phpfreaks.com/topic/136261-need-help-with-a-regex/#findComment-711440 Share on other sites More sharing options...
nrg_alpha Posted December 10, 2008 Share Posted December 10, 2008 Quick question, so I can try to understand this, what is the question mark and colon for at the begining of the grouping? Think of this as 'do not capture'. When you use grouping brackets, by default, the regex engine stores these matches (which in this case become known as captures) into variables. Every set of brackets (nested or not) stores those variables as $1, $2, $3, etc... So by simplified example, if you have a pattern like '#[a-z]+\.(.+)#' to match against the string 'dell.ca', the 'ca' part gets stored as a variable ($1). This can be handy to refer back to within your regex... but in your case, we don't want to store any values into $1, because you are simply checking (matching) for a format of an username. So instead of having the regex engine go through the trouble of storing what is grouped in the brackets as $1, we simply use (?: ) to say (do not capture).. in other words, do not store what you match into $1. This makes it lighter / faster for the regex engine. Whenever you do not need to capture, use that (?: ) notation instead. I'm going to go ahead and stay with these rules, otherwise I'm going to drive myself nuts coming up with them and it'll end up using up alot of my time coming up with them and then changing them constantly. Indeed. No use banging your head against the wall Keep it simple. Glad it worked out. Cheers, NRG Quote Link to comment https://forums.phpfreaks.com/topic/136261-need-help-with-a-regex/#findComment-711654 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.