none
How to know if string contains letters and numbers only?

    Question

  • I would like to check if there are letter and numbers in the string (but only if the string contains letters and numbers - it needs to have both),

    but I do not know to correctly write the code using Regex.

    The latters in a string are not just from an english alphabet, but there can be from my language too (č,š,ž, Č,Š,Ž, or maybe some other).

    I was trying something with:

    Match MyMatch = Regex.Match(MyString, "^[A-Z]+[a-zA-Z]+[č|š|ž|Č|Š|Ž]*$");
    
    if(!MyMatch.Success)
    {
        //
    }
    

     

    EDIT:

    I have tried with this pattern, which looks for a letter in a range between a-z and a digit. And it suits quite well to me, the only problem is how to include letters like č,š,ž,ć,đ?

    string input = "mystring123";
    
    Regex re = new Regex(@"[a-z]\d", RegexOptions.IgnoreCase);
    Match m = re.Match(input); //m has value g1
    if(!m.Success)
    {
        //if there is no letter or no number
    }
    

     

    Friday, April 02, 2010 4:31 PM

Answers

  • Regex will work fine here. If you use \w as your character class it will match the following groups of characters:

    \w Matches any word character. Equivalent to the Unicode character categories [\p{Ll}\p{Lu}\p{Lt}\p{Lo}\p{Nd}\p{Pc}]. If ECMAScript-compliant behavior is specified with the ECMAScript option, \w is equivalent to [a-zA-Z_0-9].

    These character categories can be expanded upon if you want to, here's the list of supported categories and code blocks:

    http://msdn.microsoft.com/en-us/library/20bw873z.aspx#SupportedUnicodeGeneralCategories

    http://msdn.microsoft.com/en-us/library/20bw873z.aspx#SupportedNamedBlocks

    You can string these together like this:

    ^[\p{blockone}\p{block2}]*$\

    Do note that this expression will work on framework executed code only. So placing it in a RegexValidator on ASP.NEt for example will probably not work, as ECMAScript uses a slightly different syntax. 

    With the Regex class and serverside in ASP.NET development, you should be able to use these character groups.

     

     

    • Marked as answer by Mitja Bonca Saturday, April 03, 2010 9:29 AM
    Friday, April 02, 2010 5:59 PM

All replies

  • That's going to be a little tough to do with REs.  REs can't technically handle pairs of items (or must haves) without some complex work.  Throw non-local letters into the mix and things get really complicated.  The \w class would pick up letters (including Unicode letters), digits and underscores but that isn't what you wanted.  You might do better to just enumerate the characters by hand and check like so:

    foreach(char ch in input)
    {
       if (!Char.IsLetterOrDigit(ch))
          //Error
    };

    Michael Taylor - 4/2/2010
    http://msmvps.com/blogs/p3net

    Friday, April 02, 2010 5:02 PM
  • This is not something I would like to have. 

    I would like to check if the string contains street name and the house number. Thats why the string needs to have letters and number(s).

    Friday, April 02, 2010 5:19 PM
  • What about PO boxes or military bases?  The format they use for an address is not the same.  Depending upon where in the world you are addresses can have just about any format.  But the above code I mentioned can detect that you have both letters and digits in the string (you'll just have to track that you've seen a letter and a digit).  It just doesn't confirm that the string is in a valid format for postal addresses (ie. aba 10 daba 20 duh 30).  If you are limiting your addresses to a fixed format then posting that would allow us to better provide you an answer.

    Michael Taylor - 4/2/2010
    http://msmvps.com/blogs/p3net

    Friday, April 02, 2010 5:24 PM
  • Regex will work fine here. If you use \w as your character class it will match the following groups of characters:

    \w Matches any word character. Equivalent to the Unicode character categories [\p{Ll}\p{Lu}\p{Lt}\p{Lo}\p{Nd}\p{Pc}]. If ECMAScript-compliant behavior is specified with the ECMAScript option, \w is equivalent to [a-zA-Z_0-9].

    These character categories can be expanded upon if you want to, here's the list of supported categories and code blocks:

    http://msdn.microsoft.com/en-us/library/20bw873z.aspx#SupportedUnicodeGeneralCategories

    http://msdn.microsoft.com/en-us/library/20bw873z.aspx#SupportedNamedBlocks

    You can string these together like this:

    ^[\p{blockone}\p{block2}]*$\

    Do note that this expression will work on framework executed code only. So placing it in a RegexValidator on ASP.NEt for example will probably not work, as ECMAScript uses a slightly different syntax. 

    With the Regex class and serverside in ASP.NET development, you should be able to use these character groups.

     

     

    • Marked as answer by Mitja Bonca Saturday, April 03, 2010 9:29 AM
    Friday, April 02, 2010 5:59 PM