Jump to content

preg_match


aim25

Recommended Posts

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.