locked
regular expression RRS feed

  • Question

  • Hi,

    i want a help with an expression that matches only these numbers (0,0.00,.0000,000000,0.0000)

    With Regards

    Bikram


    litu Here

    Tuesday, November 17, 2015 10:55 AM

Answers

  • Hi Dinesh

    Thanks for the help but its not matching. Its matching with 1.0 but i want a match for 0, 0.00,.000 like this


    litu Here

    I'm not quite sure of your requirement, but your requirement seems to have changed from the original (e.g. '.000' instead of '.0000'). Below are a few examples that might cater for your needs.

    ^\d{0,1}(?:\.(\d\d)+?)$

    This will match an optional digit (0-9) optionally followed by a dot followed by an even number of digits.

    If your requirement is that the first digit (before the dot) can only be a zero (or non-existing), you can replace the first '\d' by '0'.

    ^0{0,1}(?:\.(\d\d)+?)$

    If you're requirement is not that it needs to be an even number of digit after the decimal dot, you can replace '\d\d' by a single '\d'.

    ^\d{0,1}(?:\.(\d)+?)$

    If your requirement is that you only want to know if the number is zero (as in 0, .0, .00, 0.000 etc), you can replace every '\d' by a '0'.

    ^0{0,1}(?:\.(0)+?)$

    I think you will be able to derive your own one from the above if they do not cater exactly from your needs.

    • Proposed as answer by DotNet Wang Friday, November 27, 2015 9:24 AM
    • Marked as answer by Kristin Xie Monday, November 30, 2015 9:58 AM
    Tuesday, November 17, 2015 12:49 PM
  • It looks like your pattern should be something like this...

    0+(\.0+)?|\.0+

    This will accept any number of zeroes and optionally one decimal -- but not a decimal with no zeroes after it, and not a decimal by itself, and not the empty string.

    Here's how you could be more helpful in describing your requirements.  Some additional edge cases that you should explicitly indicate as being valid or not are the following:

    • ""
    • "."
    • "0."
    • "00.0"

    I also recommend using the web app at debuggex.com to help design and debug your regular expressions.

    • Proposed as answer by DotNet Wang Friday, November 27, 2015 9:24 AM
    • Marked as answer by Kristin Xie Monday, November 30, 2015 9:58 AM
    Tuesday, November 17, 2015 2:50 PM

All replies

  • Hello There,

    Please use below code

    ValidationExpression="((\d+)+(\.\d+))$"

    Cheers

    Dinesh

    • Proposed as answer by Dinesh Gabhane Tuesday, November 17, 2015 11:19 AM
    • Unproposed as answer by Dinesh Gabhane Tuesday, November 17, 2015 12:37 PM
    Tuesday, November 17, 2015 11:19 AM
  • Hi Dinesh

    Thanks for the help but its not matching. Its matching with 1.0 but i want a match for 0, 0.00,.000 like this


    litu Here

    Tuesday, November 17, 2015 11:24 AM
  • Hello There,

    Try below code

    ^[1-9]\d*(\.\d+)?$   OR

    ValidationExpression="[0-9]*\.?[0-9]*"

    Cheers

    Dinesh

    • Proposed as answer by Dinesh Gabhane Tuesday, November 17, 2015 11:30 AM
    • Unproposed as answer by Dinesh Gabhane Tuesday, November 17, 2015 12:35 PM
    Tuesday, November 17, 2015 11:29 AM
  • Hello There,

    Please use below code

    ValidationExpression="((\d+)+(\.\d+))$"

    Cheers

    Dinesh


    That does not match '.0000'; please unpropose your answer. It also matches 0.0, 0.000 etc which should fail according to OP's description.
    Tuesday, November 17, 2015 11:38 AM
  • is it really needed to match (.0000), as per mathematics rule it is equal to (0.0000)

    is it really a requirement

    Tuesday, November 17, 2015 12:37 PM
  • Hi Dinesh

    Thanks for the help but its not matching. Its matching with 1.0 but i want a match for 0, 0.00,.000 like this


    litu Here

    I'm not quite sure of your requirement, but your requirement seems to have changed from the original (e.g. '.000' instead of '.0000'). Below are a few examples that might cater for your needs.

    ^\d{0,1}(?:\.(\d\d)+?)$

    This will match an optional digit (0-9) optionally followed by a dot followed by an even number of digits.

    If your requirement is that the first digit (before the dot) can only be a zero (or non-existing), you can replace the first '\d' by '0'.

    ^0{0,1}(?:\.(\d\d)+?)$

    If you're requirement is not that it needs to be an even number of digit after the decimal dot, you can replace '\d\d' by a single '\d'.

    ^\d{0,1}(?:\.(\d)+?)$

    If your requirement is that you only want to know if the number is zero (as in 0, .0, .00, 0.000 etc), you can replace every '\d' by a '0'.

    ^0{0,1}(?:\.(0)+?)$

    I think you will be able to derive your own one from the above if they do not cater exactly from your needs.

    • Proposed as answer by DotNet Wang Friday, November 27, 2015 9:24 AM
    • Marked as answer by Kristin Xie Monday, November 30, 2015 9:58 AM
    Tuesday, November 17, 2015 12:49 PM
  • It looks like your pattern should be something like this...

    0+(\.0+)?|\.0+

    This will accept any number of zeroes and optionally one decimal -- but not a decimal with no zeroes after it, and not a decimal by itself, and not the empty string.

    Here's how you could be more helpful in describing your requirements.  Some additional edge cases that you should explicitly indicate as being valid or not are the following:

    • ""
    • "."
    • "0."
    • "00.0"

    I also recommend using the web app at debuggex.com to help design and debug your regular expressions.

    • Proposed as answer by DotNet Wang Friday, November 27, 2015 9:24 AM
    • Marked as answer by Kristin Xie Monday, November 30, 2015 9:58 AM
    Tuesday, November 17, 2015 2:50 PM