extrovertive Posted October 21, 2008 Share Posted October 21, 2008 I want a regex such that it will not accept a number of special characters in front..and letters only in front..with numbers ok. Underscore is fine. Sort of like a username type of field. Anyone help? Quote Link to comment https://forums.phpfreaks.com/topic/129453-simple-regex-help/ Share on other sites More sharing options...
rhodesa Posted October 21, 2008 Share Posted October 21, 2008 you may want to try rewording that...it's very confusing how you put it. but if i have disciphered the message correctly.... -First character must be a letter -Numers/Letters/Underscores in the rest are fine ^[a-zA-Z]\w*$ Quote Link to comment https://forums.phpfreaks.com/topic/129453-simple-regex-help/#findComment-671176 Share on other sites More sharing options...
The Little Guy Posted October 24, 2008 Share Posted October 24, 2008 I thing he also wants to allow numbers and underscores: /^(_|[a-zA-Z0-9])\w*$/ Quote Link to comment https://forums.phpfreaks.com/topic/129453-simple-regex-help/#findComment-673879 Share on other sites More sharing options...
nrg_alpha Posted October 24, 2008 Share Posted October 24, 2008 I thing he also wants to allow numbers and underscores: /^(_|[a-zA-Z0-9])\w*$/ There is no need for alternations in this case.. you can simply use \w throughout (assuming it can start with an underscore or any number or letter: #^\w+$# Granted, depending on the locale, \w actually can match more than simply a-zA-Z0-9_.. it can also potentially match characters like à ò ì ù è by example (there are even more). So if it can start with an underscore, letter or number and must be any and only any of those throughout the string, then a simple character class with those would suffice: #^[a-z0-9_]+$#i Quote Link to comment https://forums.phpfreaks.com/topic/129453-simple-regex-help/#findComment-674037 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.