Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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,

Posted
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})

Posted

Ok, here's what I did, and it worked:

 

       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:

CASH    1.00     0.21

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...