最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - Regex pattern allowing alphabets, numbers, decimals, spaces and underscore - Stack Overflow

programmeradmin3浏览0评论

I am not good with regular expression patterns. I have to put a validation on an string, to allow

only alphabets, numbers, decimals, spaces, ma and underscore 

for allowing the alphabets and spaces I have /^[a-zA-Z][a-zA-Z\\s]+$/ Please help me in creating all the above conditions in one pattern.

Thanks

I am not good with regular expression patterns. I have to put a validation on an string, to allow

only alphabets, numbers, decimals, spaces, ma and underscore 

for allowing the alphabets and spaces I have /^[a-zA-Z][a-zA-Z\\s]+$/ Please help me in creating all the above conditions in one pattern.

Thanks

Share Improve this question edited Dec 29, 2012 at 20:57 Sashi Kant asked Dec 26, 2012 at 13:30 Sashi KantSashi Kant 13.5k9 gold badges49 silver badges71 bronze badges 2
  • Not sure why you added a jQuery tag... And what is the difference between numbers and decimals. – PhiLho Commented Dec 26, 2012 at 13:34
  • Is "decimals" the decimal point? I added it, just in case. – PhiLho Commented Dec 26, 2012 at 13:37
Add a ment  | 

3 Answers 3

Reset to default 8

this regex should work for your requirements

'[a-zA-Z0-9_. ,]*'

In the regex, I specified the range a to z, A to Z (uppercase), 0 to 9 and the single character _, decimal point ".", space and a ma.

If you want to make sure you want at least one character after the first letter, you can replace the * with a +, or {2,} with at least 2 more characters, or {2,5} with between 2 and 5 characters.

You can try:

/^[\w., ]+$/

I don't know what are the requirements for the starting char, if there are any.

Rahul's answer gave me the direction to think, but for the visitors, may be this too can be helpfull

patternForClasName      =   /^([a-zA-Z0-9 _]+\.)*[a-zA-Z0-9 _]+$/;  
// Allowing valid className which has a format abcsasa.dsd.dsd(the class or a package name can have an underscore or a numerical)

patternForName          =   /^([a-zA-Z0-9 _-]+)$/;
// Allowing alphanumeric + spaces + (_)underscore and a (-)dash

patternForDescription   =   /^([a-zA-Z0-9 _-]+[\,\.]+)*[a-zA-Z0-9 _-]*$/;
  // Allowing alphanumeric,spaces, (_)underscore, (-)dash, ma, decimal

patternURLFormat        = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/;

// For a valid URL

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论