Correctly Matching a Regular Expression for a specific formatted string (C# 2005)

Shaitan00

Well-known member
Joined
Aug 11, 2003
Messages
343
Location
Hell
I am trying to find a way to correctly use Regular Expression (RegEx) to decompose a specific formatted string into 3 values, here are 3 samples of the strings in question.
"1 CASH 1.00 $ 0.21 $"
"1 CASH 1.50 $ 0.22 $"
"1 CASH 10.50 $ 100.50 $"
.. etc ..
This format will never change, my goal is to be able to automatically decompose each string into 3 main fields which represent 1=Type (Cash), 2=Value (1.00), 3=Value (0.21).

So far I was able to generate the following regular expression:
^[0-9]+[ ]+(.+)[ ]+[0-9]+\.[0-9]{2}[ ]+\$[ ]+([0-9]+\.[0-9]{2})[ ]+\$

This splits/decomposes my string into "CASH" and "0.21" (using the first string as an example) whereas I expected (and need) it to be split into 3 values "CASH", "1.00", and "0.21". Why is the "1.00" not picked up? What I am doing wrong? Using the first string as an example I would have expected

Any help, hints, or advice would be greatly appreciated.

(I am using Visual Studios C# 2005)
Thanks,
 
So far I was able to generate the following regular expression:
^[0-9]+[ ]+(.+)[ ]+[0-9]+\.[0-9]{2}[ ]+\$[ ]+([0-9]+\.[0-9]{2})[ ]+\$

Try enclosing the bolded expression in parantheses: ([0-9]+\.[0-9]{2})
 
Ok, heres what I did, and it worked:

Code:
        Dim x As New System.Text.RegularExpressions.Regex("^[0-9]+[ ]+(.+)[ ]+([0-9]+\.[0-9]{2})[ ]+\$[ ]+([0-9]+\.[0-9]{2})[ ]+\$")

        Dim y() As String

        y = x.Split("1 CASH 1.00 $ 0.21 $")

        For Each s As String In y
            MessageBox.Show(s)
        Next

The result is 5 strings, two spaces and:
Code:
CASH    1.00     0.21
 
Back
Top