locked
c# regular expression to only allow 1 or 2 digits only RRS feed

  • Question

  • User1332640826 posted

    I will admit i know nothing about regular expressions. what I am trying to do is use a regular expression which expects only 1 or 2 digits.

    Sample

    1

    23

    Tuesday, December 16, 2014 10:10 AM

Answers

  • User281315223 posted

    If you want a very simple regular expression to match one or two digits only, you just need the following :

    ^\d{1,2}$

    This will explicitly match a string that only contains 1 or two digits. If you wanted to use it to match multiple instances of one or two digits within a string, then you could just leave off the beginning '^' and end '$' characters as seen below :

    \d{1,2}

    You can see a working example here or demonstrated below :

    // An example string
    var example = "There will be 1 sentence behind this. The answer to life, the universe and everything is 42, but I am 29 years old.";
    		
    // Use a Regex to match any 1 or two digit numbers
    var matches = Regex.Matches(example,@"\d{1,2}"); // This will contain "1", "42" and "29"
    		

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, December 16, 2014 10:42 AM

All replies

  • User-1506965535 posted

    If you are using asp.net then you can define something like this

    <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="Please enter only two digit" ControlToValidate="yourtextboxid" ValidationExpression="^[0-9]{2}$"></asp:RegularExpressionValidator>

    Tuesday, December 16, 2014 10:16 AM
  • User1332640826 posted

    That's the one i am using , but currently it accepts 2 digits. I need expression which accepts one or two digits.

    Tuesday, December 16, 2014 10:23 AM
  • User-1506965535 posted

    OK,

    try with this:-

    /^\d{1,2}$/;

    Tuesday, December 16, 2014 10:27 AM
  • User281315223 posted

    If you want a very simple regular expression to match one or two digits only, you just need the following :

    ^\d{1,2}$

    This will explicitly match a string that only contains 1 or two digits. If you wanted to use it to match multiple instances of one or two digits within a string, then you could just leave off the beginning '^' and end '$' characters as seen below :

    \d{1,2}

    You can see a working example here or demonstrated below :

    // An example string
    var example = "There will be 1 sentence behind this. The answer to life, the universe and everything is 42, but I am 29 years old.";
    		
    // Use a Regex to match any 1 or two digit numbers
    var matches = Regex.Matches(example,@"\d{1,2}"); // This will contain "1", "42" and "29"
    		

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, December 16, 2014 10:42 AM
  • User1332640826 posted

    nope it dint work

    Tuesday, December 16, 2014 10:42 AM
  • User-1506965535 posted

    Can you post your html code ?

    Tuesday, December 16, 2014 10:43 AM
  • User281315223 posted

    nope it dint work

    Did you try the code that I provided earlier? Additionally, could you post an example of how you are currently using this? The expression can be further explained a bit more as follows :

    ^           # Beginning of expression
    \d{1,2}     # Explicit match one or two ({1,2}) digits (\d is equivalent to [0-9])
    $           # End of expression

    Tuesday, December 16, 2014 10:47 AM
  • User1332640826 posted

    thanks ! worked perfectly

    Tuesday, December 16, 2014 10:51 AM