Jump to content

Simple question...


TheFilmGod

Recommended Posts

Think you would have learned by now how saying "doesn't work" is not helpful...

 

anyways, most immediate thing I see wrong is that you probably want to move the hyphen to the beginning or end of that last character class.  As it stands right now, you are telling it to match on range of . to @

 

if that doesn't fix whatever "doesn't work", I suggest you provide more detail as to what you mean by "doesn't work."

 

p.s.- thread moved.  Another thing I would have thought you would know by now...

Link to comment
Share on other sites

If you aren't sure about what special characters need to be escaped, it would have taken about 5 minutes to verify for yourself. Just create the regex with simple alphanumeric checking then add in one special character at a time and test accordingly. I see that you posted about an hour ago. I'll go test this myself after I submit this to see how long it takes for me to figure it out through trial and error.

Link to comment
Share on other sites

The dash and dollar sign needed to be escaped.

 

preg_match('/^[A-Za-z0-9_][A-Za-z0-9_.-@#$!]{4,49}$/', $username)

 

Edit: You can simplify that by using the character class \w which is the same as [A-Za-z0-9_]

 

preg_match('/^[\w][\w.-@#$!]{4,49}$/', $username)

Link to comment
Share on other sites

The dash and dollar sign needed to be escaped.

 

preg_match('/^[A-Za-z0-9_][A-Za-z0-9_.-@#$!]{4,49}$/', $username)

 

Edit: You can simplify that by using the character class \w which is the same as [A-Za-z0-9_]

 

preg_match('/^[\w][\w.-@#$!]{4,49}$/', $username)

 

Actually, characters like the dash or dollar sign doesn't need to be escaped. In the case of the dash, so long as it is listed as either the first or last character in the character class, it is treated as a literal as opposed to a range.

 

Regarding the \w shorthand character class, it may match more than you bargained for, depending on your locale.

Link to comment
Share on other sites

Actually, characters like the dash or dollar sign doesn't need to be escaped. In the case of the dash, so long as it is listed as either the first or last character in the character class, it is treated as a literal as opposed to a range.

 

Regarding the \w shorthand character class, it may match more than you bargained for, depending on your locale.

 

About the dash, you are right, but it's easier for me to remember to always escape it (like I have to do for other characters) than to remember a special rule for it. Either way works.

 

As for the locale and the character classes, that's a new one for me. Never ran into that problem before.

Link to comment
Share on other sites

In character classes, most meta characters lose their special abilities. Those that could retain them largely dependent on their location within the class (characters like ^ or - for example). Granted, there is no harm in escaping, as you said, they will work. Call me a sucker for keeping patterns as clean as possible..(read: less characters = an 'easier' looking pattern  :tease-03:) so if I can place the dash last, that's one less thing to escape.. much like the choice of delimiters.. most commonly, the / character is used.. but this means that listing that character in the pattern requires escaping (regardless whether it's in a class or not), so I use a different delimiter instead. Just makes less need for escaping. Using modifiers like i for case insensitivity reduces the need to list upper and lowercase letters and such.. so less is more appealing and easier on the eyes to read. That's just me though.

 

Yeah, with regards to the locale, depending on yours, \w, \d, and ctype functions a la ctype_alnum() and friends may not be an issue. I know for me it is (in that, if I don't set my LC_TYPE to 'C' first, I leave the door open for potential exponents, accented characters and the like.. a real pain imo). So it may or may not be an issue for you. You'd have to test to see if it is an actual issue or not. Obviously, sticking to character classes (like [0-9] as opposed to \d will pretty much guarantee no exponents for example if not inclined to set LC_TYPE). As usual, different strokes for different folks I suppose.

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.