Click here to Skip to main content
15,908,173 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
I have a string which i need to parse using Regex class of Syste.Text.Regularexpressions.
I need to find if the first 2 characters of the string are either "00" or "07" or "16" or "23".
Please let me know as what the pattern would be to match the above.

thanks to all who helped and also who viewed my query.
Posted

hello,

You can try something like this. Please refer to SAKryukov post to read the reference.

C#
string regMatch = @"^(00|07|16|23)";
       if (System.Text.RegularExpressions.Regex.IsMatch("00test", regMatch))
       {
           Response.Write( "valid");
       }
       else
       {
           Response.Write( "invalid!");
       }
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 3-Mar-11 22:47pm    
I forgot the criteria "first two characters", so your answer is correct, my 5, and mine needs a fix.
--SA
Bryian Tan 4-Mar-11 20:30pm    
hi SAKryukov, Thanks :)
^(00|07|16|23)


Was that really so hard to read the reference? (Such as http://www.regular-expressions.info/reference.html[^])

[EDIT] The pattern was corrected later as I forgot the criterion "first 2 characters".
Bryian Tan, thank you for correct Answer.

—SA
 
Share this answer
 
v5
If you don't know REGEX, you may not use it for such simple tasks.
Try instead this clearer approach:
private static bool TestString(string s)
{
    if (s.Length < 2) return false;
    switch (s.Remove(2))
    {
        case "00":
        case "07":
        case "16":
        case "23": return true;
        default: return false;
    }
}


Not everything should be done with regexes!
 
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