locked
Reqular Expression for Alphanumeric RRS feed

  • Question

  • Hi,

    I am newbie in regular expressions.
    Please help me in writing a regular expression for a String that matches following criteria

    Alphanumeric (hyphen, apostrophe, period, and space allowed)
    Hyphen not allowed at the start or end
    Period and  apostrophe not allowed at the start.
    • Edited by renuka_kale Thursday, August 21, 2008 7:58 AM extra characters added
    Thursday, August 21, 2008 7:45 AM

Answers

  • ^(([a-zA-Z0-9\s][a-zA-Z0-9\s'.-]*[a-zA-Z0-9'.\s])|([a-zA-Z0-9\s]))$

    The requirement is that the the string matches the criteria, so I use ^ and $ to bound the pattern and keep the pattern from matching strings that "contain" the criteria.

    The [a-zA-Z0-9\s] syntax matches a single character in any of the three ranges a-z, A-Z, 0-9 or a space character \s (which could be a tab, "space" in Regex usually means white space, use " " for the space bar character).  This is the newbie syntax in that it is very explicit.  You could also use \d as a short hand for 0-9.

    The [a-zA-Z0-9\s'.-]* syntax matches zero or more characters that match the pattern.  It's the same pattern with apostrophe, dot (period), and hyphen added.

    The final [...] group matches one character.  The hyphen has been removed.

    There are two groups or'ed (|) together because there is some ambiguity about number of characters.  I assume one character is okay, but if the string is one character iand the character is hyphen, apostrophe or dot, then the match should fail.

    Here is my test code...

                string pattern = @"^(([a-zA-Z0-9\s][a-zA-Z0-9\s'.-]*[a-zA-Z0-9'.\s])|([a-zA-Z0-9\s]))$";  
                string[] tests = {  
                                      "!!!! this is a test !!!!"//false  
                                      "++++++++"// false  
                                      "a"// true  
                                      "ab"// true  
                                      "1"// true  
                                      "1a2"// true  
                                      ".a"// false  
                                      "-b"// false  
                                      "'"// false  
                                      "a'"// true  
                                      "1.2"// true  
                                      "abc.efg"// true  
                                  };  
                foreach (string test in tests)  
                    Console.WriteLine("{0}  => {1}", Regex.IsMatch(test, pattern).ToString(), test);  
     

    and here is the output...

    False  => !!!! this is a test !!!!
    False  => ++++++++
    True  => a
    True  => ab
    True  => 1
    True  => 1a2
    False  => .a
    False  => -b
    False  => '
    True  => a'
    True  => 1.2
    True  => abc.efg

    This can be done in many ways and with fewer characters, but since you are a newbie, these are what I consider some basics you should know...

        []
        *
        ^$
        ()|()


    Les Potter, Xalnix Corporation, Yet Another C# Blog
    • Proposed as answer by VHurdugaci Thursday, August 21, 2008 5:36 PM
    • Marked as answer by OmegaManModerator Sunday, September 7, 2008 2:45 AM
    Thursday, August 21, 2008 2:20 PM