Jump to content

Recommended Posts

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;

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/136261-need-help-with-a-regex/
Share on other sites

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]$

 

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

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.

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? 

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.

 

 

 

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.

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 ;)

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.

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.