Home > DeveloperSection > Forums > Find first comma in string, then extract value between spaces
Jeet Verma
Jeet Verma

Total Post:110

Posted on    December-22-2014 10:45 PM

 ASP.Net C# 

 1 Reply(s)
 1393  View(s)
Rate this:

I'm extracting rows from a txt file.

This row contains values like this:

 DESCRIPTION 1    1.234,00    15.980,00 [etc.]

I would like to extract these values (I mean only numeric values).

So I thought to find first comma, execute a for cycle backwards until first White space and execute a For cycle forward for decimals digits. The I should go to the second comma and perform these cycles again.

Can you suggest some code that could be useful for my solution?

marcel ethan
marcel ethan

Total Post:105

Posted on    December-23-2014 5:27 AM

From your description, if you just need the decimal number before the comma, then you can do this with a pretty simple regex:

Dim s = "DESCRIPTION 1    1.234,00    15.980,00"

Dim pattern = "\d+(\.\d+)?,\d+"

Dim matches = System.Text.RegularExpressions.Regex.Matches(s, pattern)


For Each match in matches








Here's a quick breakdown of the regex:

\d+ - \d is shorthand for [0-9], which just means "any numeric character". The + just indicates "one or more"

\. - this just matches a period character.

, - this just matches a comma.

( ... ) - parentheses just creates a group (think of it as a sub-regex)

? - question marks mean that the previous item is optional. In this case, that means that the group matching (\.\d+)? is optional, which allows you to match both 0.000,00 and 0,00

In that regex, if the comma and period are optional, then you can add a ? after them.

Don't want to miss updates? Please click the below button!

Follow MindStick