-1

I am looking for a regex that allows decimal numbers and - only. I want the decimal to allow for two decimal places(.00). the regex I wrote only accepts decimals not -. any help on this would be great!

My regex code:

    If Regex.IsMatch(node1("Comission").InnerText, "^[-]|\s\d{0,10}(\.\d{1,2})?$") = False Then
        lstNodeErrs.Add(" invalid Comission:  " & Comission)
    End If

xml

<?xml version="1.0" encoding="us-ascii" standalone="yes"?>
<audit_xml>
    <DataTable>     
        <Total_Debit_Amt>11.39</Total_Debit_Amt>
        <Comission>11.39</Comission>
        <Total_Taxes> </Total_Taxes>    
        <Tax_on_Commission_Amount> - </Tax_on_Commission_Amount>
    </DataTable>

Example data: valid:

 14
 154542.75
 .91
 0.00
 -
 8.57

invalid:

12.1233
fhjfjfjh
ghghg.kk

note:comission can be decimal empty or -

789
  • 3
  • 4
  • Possible duplicate of [Simple regular expression for a decimal with a precision of 2](https://stackoverflow.com/questions/308122/simple-regular-expression-for-a-decimal-with-a-precision-of-2) – Basel Issmail Mar 12 '19 at 12:29

1 Answers1

0

Does this work?

^\d{0,10}(\.[0-9-]{1,2})?$

Please share some example data if this does not work.

Mario Jost
  • 230
  • 1
  • 7
  • dont know if I understood your question correctly. here is my test: https://regex101.com/r/zFjWnM/1 – Mario Jost Mar 12 '19 at 12:17
  • Something like this should work then? `^[-]|\d{0,10}(\.\d{1,2})?$` Check out: https://regex101.com/r/zFjWnM/3 – Mario Jost Mar 12 '19 at 12:28
  • Since you formated your testdata, i could refine the regex more: `^[-]?\d+\.?(\d{1,2})?$` Before, it was either a match for or a positive number. Now it matches all positive and negative numbers with or without 2 decimal values. Have a look at: https://regex101.com/r/zFjWnM/4 – Mario Jost Mar 12 '19 at 12:55
  • I have edited my code to make it more clear what I want to be valid and invalid. your regex: ^[-]|\d{0,10}(\.\d{1,2})?$ worked great except it allowed more than 3 decimal places to be valid(123.12356-valid but should be invalid) – 789 Mar 12 '19 at 13:22
  • So this regex should do it then: `^[-]?\d+\.?(?:\d{1,2})?$` https://regex101.com/r/zFjWnM/6 I removed capturing of groups, as you dont pointed out you need them. The code is more clean like this. – Mario Jost Mar 12 '19 at 14:16
  • Just saw that you want to match entries without a first digit like .12 so i adjusted the regex to this: `^[-]?(?:\d+)?\.?(?:\d{1,2})?$` Hope this is the final verison of it: https://regex101.com/r/zFjWnM/7 – Mario Jost Mar 12 '19 at 14:23
  • I have spaces between some of my data does this mean I have to put \s into my regex or do new line spaces matter? thanks so much for your help. – 789 Mar 12 '19 at 14:48
  • If you have leading spaces in front of the numbers, just delete the ^ symbol (first character of the regex) and it should work. – Mario Jost Mar 12 '19 at 15:04
  • Or if you wanna go the approach with the \s you can go with following regex: `^\s*(?:[-])?(?:\d+)?\.?(?:\d{1,2})?$`Example: https://regex101.com/r/zFjWnM/9 – Mario Jost Mar 12 '19 at 15:18
  • If you wanna have a shorter version of the regex you can go with: `^\s*-?\d*\.?\d{0,2}$` It works the same, but is harder to read in my opinion. Example: https://regex101.com/r/zFjWnM/10 – Mario Jost Mar 12 '19 at 15:24
  • this works perfect in the online regex code tester but for my actual code if It validates123.456789 – 789 Mar 12 '19 at 15:40
  • I have added more code to give a better idea of what im trying to do – 789 Mar 12 '19 at 15:44
  • I tested the regex on this .NET regex testsite: http://regexstorm.net/tester and it worked. But you can only check oneliners here. But looking at your code, it seems you are checking one line anyway. So the only idea i have is to maybe drop the ^ in the front and only use: `\s*-?\d*\.?\d{0,2}$` But other than that, i'm sorry i can't help you out any further. You still need the $ at the end to 'NotMatch' the entries that have 3 or more decimal digits. – Mario Jost Mar 12 '19 at 17:37