Interpreting a VB.net regular expression

brgdotnet
brgdotnet used Ask the Experts™
on
I have the following regular expression which is in some VB.net code I am maintaining. I know that it is used for formatting amounts of money.
Can someone interpret exactly what the regular expression represents as far as digits, decimal places etc. or whatever?

^([0-9]+|[0-9]{1,3}(,[0-9]{3})*)(\.[0-9]{1,4})?$


In VB.net :
Dim myMoneyRegEx As String ="^([0-9]+|[0-9]{1,3}(,[0-9]{3})*)(\.[0-9]{1,4})?$"
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
IT Guru
Most Valuable Expert 2011
Commented:
At least one digit, possibly with commas (in the correct place as used for thousands), with up to 4 decimal places.

Valid:
0
1000000
1,000,000
99999999000000
99,999,999,000,000
1000.1234
1000.0001
0.0001
0.0000

Invalid:
(blank)
.1
.0001
1.00001
,999
10,00
Commented:
You can also use regex101.com in order to determine what your regular expression does and test for matches.Capture.PNG-saige-
here is an example you might like to use sometime:
 Public Sub CheckCurrency()
        Dim rx As New Regex("^-?\d+(\.\d{2})?$")
        Dim values() As String = {"-42", "19.99", "0.001", "100 USD"}

        For Each value As String In values
            If rx.IsMatch(value) Then
                Console.WriteLine("{0} is a currency value.", value)
            Else
                Console.WriteLine("{0} is not a currency value.", value)
            End If
        Next
    End Sub

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial