Jump to content

Recommended Posts

I have the below script snippet that I found on the web, I like it, does what I ask of it, however I have some questions regarding it's logic if you will.

 

Here is the preg_match:

 

preg_match('/^[A-Za-z](?=[A-Za-z0-9_.]{3,31}$)[a-zA-Z0-9_]*\.?[a-zA-Z0-9_]*$/', $input);

 

I know that the caret ^ is the start of the pattern that it must match, that being all alpha characters only? i.e. all the way from a to z and then in upper-case as well?

 

? being (I can't remember that one exactly, equaling anything from a to z and then an underscore and a dot (.) which means anywhere in the string right? (I feel I am learning allot from the previous examples of Regex's, just need some help).

 

The next one is 3 to 31 characters in length?

 

The $ is the end of the pattern, have I got this right?

 

I am confused as to what the rest of the logic does, wouldn't it make more sense just to have the first bit, I know it's a question based on what I would want it to do, it's just allow say the pattern up to the character class at the start?

 

I mean I would have to change the {3,31} and change it to any length I wish, I would say a min of 5 chars then a maximum of 35.

 

Any help is appreciated,

Jez.

You're kinda there.

 

The expression does a couple things: enforces a length requirement (4-32 characters long) and enforces a pattern constraint (letter + letters/digits/underscores + period maybe + letters/digits/underscores.

The ^[A-Za-z] is that leading letter. The (?= means the string has to match some pattern at that point, but if it does then the rest of the pattern is evaluated at that same point (it doesn't "use up" characters); thus the string must have somewhere between 3 and 31 more letters, numbers, underscores, or periods, and then the end of the string. If that works then there's the stricter requirement: any amount of letters, numbers, or underscores, maybe a period, and then more lettters, numbers, and underscores. (And then the end of the string.)

 

If you want a length of 5-35 then use {4,34} instead of the {3,31} that's there. (Note how the "missing" character was already counted at the start of the expression.)

Oh that's wonderful!

 

Will go back over this in a couple of hours, but I kind of get it, makes sense when you look at the logic compared to what you have just said.

 

Will go back over some other examples and compare (how I do well with learning to be honest).

 

Much appreciated!

Jez.

Here's how RegexBuddy summarizes that:

 

 

^[A-Za-z](?=[A-Za-z0-9_.]{3,31}$)[a-zA-Z0-9_]*\.?[a-zA-Z0-9_]*$

 

Assert position at the beginning of the string «^»

Match a single character present in the list below «[A-Za-z]»

  A character in the range between “A” and “Z” «A-Z»

  A character in the range between “a” and “z” «a-z»

Assert that the regex below can be matched, starting at this position (positive lookahead) «(?=[A-Za-z0-9_.]{3,31}$)»

  Match a single character present in the list below «[A-Za-z0-9_.]{3,31}»

      Between 3 and 31 times, as many times as possible, giving back as needed (greedy) «{3,31}»

      A character in the range between “A” and “Z” «A-Z»

      A character in the range between “a” and “z” «a-z»

      A character in the range between “0” and “9” «0-9»

      One of the characters “_.” «_.»

  Assert position at the end of the string (or before the line break at the end of the string, if any) «$»

Match a single character present in the list below «[a-zA-Z0-9_]*»

  Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»

  A character in the range between “a” and “z” «a-z»

  A character in the range between “A” and “Z” «A-Z»

  A character in the range between “0” and “9” «0-9»

  The character “_” «_»

Match the character “.” literally «\.?»

  Between zero and one times, as many times as possible, giving back as needed (greedy) «?»

Match a single character present in the list below «[a-zA-Z0-9_]*»

  Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»

  A character in the range between “a” and “z” «a-z»

  A character in the range between “A” and “Z” «A-Z»

  A character in the range between “0” and “9” «0-9»

  The character “_” «_»

Assert position at the end of the string (or before the line break at the end of the string, if any) «$»

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.