Click here to Skip to main content
15,887,175 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
Can some one help me in generating the regular expression for string up to 30 characters long of letters, numbers, and/or spaces

Thanks in advance

What I have tried:

i tried with this regualr expression
@"^[0-9a-zA-Z\s]{0,30}$/"
Posted
Updated 12-Dec-16 15:07pm
v2
Comments
#realJSOP 12-Dec-16 14:54pm    
Regex? Really? That's kinda like killing a flea with a sledge hammer.

And if I try the Regex, it works - except it has a "/" character after the "end of string" indicator. Remove that, and it should work:
C#
@"^[0-9a-zA-Z\s]{0,30}$"
 
Share this answer
 
Comments
Member 12245369 12-Dec-16 15:06pm    
Thank You
OriginalGriff 12-Dec-16 16:02pm    
You're welcome!
Why not just write a string extension method that uses regular .Net classes/methods. it would be much more efficient than using Regex.

C#
public static bool ValidString(this string text)
{
    bool result = true;
    if (text.Length <= 30)
    {
        string lowCaseText = text.ToLower();
        string validChars = "abcdefghijklmnopqrstuvwxyz 1234567890";
        for (int i = 0; i < text.Length; i++)
        {
            if (!validChars.Contains(text[i]))
            {
                result = false;
                break;
            }
        }
    }
    else
    {
        result = false;
    }
    return result;
}
 
Share this answer
 
v3
Comments
Member 12245369 12-Dec-16 15:06pm    
Thank you very much
Here is a few links that can help to use RegEx
Here is a link to RegEx documentation:
perlre - perldoc.perl.org[^]
Here is links to tools to help build RegEx and debug them:
.NET Regex Tester - Regex Storm[^]
Expresso Regular Expression Tool[^]
This one show you the RegEx as a nice graph which is really helpful to understand what is doing a RegEx:
Debuggex: Online visual regex tester. JavaScript, Python, and PCRE.[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900