TheFilmGod Posted July 23, 2009 Share Posted July 23, 2009 preg_match('/^[A-Za-z0-9_][A-Za-z0-9_.-@#$!]{4,49}$/', $username) Regex doesn't work. Take notice of the special characters. Do I need to escape any of them? Quote Link to comment https://forums.phpfreaks.com/topic/167190-simple-question/ Share on other sites More sharing options...
.josh Posted July 23, 2009 Share Posted July 23, 2009 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... Quote Link to comment https://forums.phpfreaks.com/topic/167190-simple-question/#findComment-881544 Share on other sites More sharing options...
Psycho Posted July 23, 2009 Share Posted July 23, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/167190-simple-question/#findComment-881547 Share on other sites More sharing options...
Psycho Posted July 23, 2009 Share Posted July 23, 2009 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) Quote Link to comment https://forums.phpfreaks.com/topic/167190-simple-question/#findComment-881552 Share on other sites More sharing options...
nrg_alpha Posted July 23, 2009 Share Posted July 23, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/167190-simple-question/#findComment-881561 Share on other sites More sharing options...
Psycho Posted July 24, 2009 Share Posted July 24, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/167190-simple-question/#findComment-881625 Share on other sites More sharing options...
nrg_alpha Posted July 24, 2009 Share Posted July 24, 2009 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 ) 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. Quote Link to comment https://forums.phpfreaks.com/topic/167190-simple-question/#findComment-881642 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.