---
title: "Find first comma in string, then extract value between spaces"  
description: "Find first comma in string, then extract value between spaces"  
author: "Anonymous User"  
published: 2014-12-22  
updated: 2014-12-23  
canonical: https://www.mindstick.com/forum/12810/find-first-comma-in-string-then-extract-value-between-spaces  
category: "asp.net"  
tags: ["c#"]  
reading_time: 2 minutes  

---

# Find first comma in string, then extract value between spaces

I'm extracting rows from a [txt file](https://www.mindstick.com/forum/12829/read-a-txt-file-and-return-a-list-of-words-with-their-frequency-in-the-file).

This row contains [values](https://www.mindstick.com/forum/327/sum-textbox-values) like this:

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

I would like to [extract](https://www.mindstick.com/forum/12883/how-to-extract-sub-string-between-two-string-pattern-using-javascript-jquery) these values (I mean only numeric values).

So I thought to find first comma, [execute](https://www.mindstick.com/interview/49/how-can-i-execute-a-php-script-using-command-line) a for [cycle](https://yourviews.mindstick.com/view/81850/possible-effects-of-solar-weather-cycle) backwards until first White [space](https://www.mindstick.com/articles/12954/do-your-electrical-repair-in-your-own-space) and execute a For cycle [forward](https://www.mindstick.com/forum/33488/in-webview-disabling-uitoolbar-s-back-and-forward-butttons) for decimals digits. The I should go to the [second](https://answers.mindstick.com/qa/41761/how-did-the-second-industrial-revolution-influence-women-s-roles-in-society) comma and perform these cycles again.

Can you suggest some [code](https://yourviews.mindstick.com/view/85458/alan-turing-the-mastermind-behind-cracking-the-enigma-code-during-world-war-ii) that could be [useful](https://www.mindstick.com/articles/12694/how-dolomite-mineral-is-useful-in-human-life) for my solution?

## Replies

### Reply by marcel ethan

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

Console.WriteLine(match.Value)

Next

'Outputs:

'

'1.234,00

'15.980,00

---

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.


---

Original Source: https://www.mindstick.com/forum/12810/find-first-comma-in-string-then-extract-value-between-spaces

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
