locked
regex for two characters only RRS feed

  • Question

  • Hello,

    1-
    What is the regex for only two characters i.e.
    x1
    xx
    22

    Thank  you

    Wednesday, November 15, 2017 1:09 PM

Answers

  • Hello arkiboys,

    Try the following code:

     string s1 = "2r";
      
     string regex1 = @"^[A-Za-z0-9]{2}$";
    
     Boolean isValild1 = Regex.IsMatch(s1, regex1);

    Sincerely,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Proposed as answer by Sabah ShariqMVP Thursday, November 16, 2017 10:14 AM
    • Marked as answer by arkiboys Sunday, November 26, 2017 3:23 PM
    Thursday, November 16, 2017 9:24 AM

All replies

  • When you want to limit the # of characters to an exact amount you use the {2} syntax.

    var pattern = "\w{2}";


    Michael Taylor http://www.michaeltaylorp3.net

    • Proposed as answer by Sabah ShariqMVP Thursday, November 16, 2017 10:14 AM
    Wednesday, November 15, 2017 2:59 PM
  • Hello arkiboys,

    Try the following code:

     string s1 = "2r";
      
     string regex1 = @"^[A-Za-z0-9]{2}$";
    
     Boolean isValild1 = Regex.IsMatch(s1, regex1);

    Sincerely,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Proposed as answer by Sabah ShariqMVP Thursday, November 16, 2017 10:14 AM
    • Marked as answer by arkiboys Sunday, November 26, 2017 3:23 PM
    Thursday, November 16, 2017 9:24 AM
  • Hello arkiboys,

    Did you have anything to update?  If you already have got the useful solution from there, you could close the thread by marking the useful reply  as answer, This can be beneficial to other community members reading this thread. Thanks a lot.

    Sincerely,
    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Sunday, November 26, 2017 11:51 AM