Ajinkya Thorat
Ajinkya Thorat

Reputation: 9

Get decimal value from string using regex C#

I want to extract decimal value of input string

Total (pre tax) 93.78 EUR

I tried with

Regex.Replace(string input, "[^0-9]+", string.Empty)

but it only extracted 9370 where the expected outcome is 93.78.

Kindly help me with the pattern to get decimal value.

Upvotes: 1

Views: 3806

Answers (6)

Ahmer Afzal
Ahmer Afzal

Reputation: 499

string input = "Java JDK 12.0.1";
var result = Regex.Matches(input, @"[0-9]+(\.[0-9]\.[0-9]+)?");

result: 12.0.1

Upvotes: 0

Magnetron
Magnetron

Reputation: 8543

For integer or float:

string result = Regex.Match(input,@"[0-9]+(\.[0-9]+)?").Value;

for only floats:

string result = Regex.Match(input,@"[0-9]+\.[0-9]+").Value;

Upvotes: 1

Prasad Telkikar
Prasad Telkikar

Reputation: 16049

Regex.Split() will extract all floating values from input string and stores it into string[], as simple as string.Split function

you can try with this:

string stringInput = "Total (pre tax) 93.78 EUR";
string[] splitValue = Regex.Split (stringInput , @"[^0-9\.]+");
foreach(string item in splitValue)
{
    //Here you can convert it to decimal
    Console.WriteLine(item);
}

Output:

93.78

DotnetFiddler

Upvotes: 0

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726489

You could use your approach as a quick hack if you add '.' to the list of characters that you wish to keep, i.e. [^0-9.]. However, this would not be robust enough, because it would keep other digits, e.g.

Total (inclusive of 20% VAT) 93.78 EUR

would yield 2093.78, which is not what you are looking for.

A better approach is to use a regex that is specific for the price, for example

@"(\d+[.,]\d\d) EUR"

would match a number with two decimal digits, when it is followed by EUR.

Upvotes: 1

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186668

I suggest matching instead of replacing: let's extract the value of interest, instead of deleting all the other characters.

string result = Regex.Match(
     "Total (pre tax) 93.78 EUR", 
    @"[0-9]+(\.[0-9]+)?")
  .Value;

Upvotes: 5

Jon Skeet
Jon Skeet

Reputation: 1499950

You're currently replacing everything that isn't a digit - and that includes ..

I would suggest instead that you capture groups of digits with optional "dot followed by more digits". That way you'll be able to capture more than one value from text as well - or reject it if you need to, based on whatever criteria you have. Here's an example:

using System;
using System.Text.RegularExpressions;

class Program
{
    public static void Main()        
    {
        string text = "I start with 5 and take away 2.52 to get 2.48 as a result";
        Regex regex = new Regex(@"\d+(\.\d+)?");
        var matches = regex.Matches(text);
        foreach (Match match in matches)
        {
            Console.WriteLine(match.Value);
        }
    }
}

Output:

5
2.52
2.48

You can use MatchCollection.Count to determine how many matches there are - we don't know your context, but you may well want to take different actions depending on whether there are no matches, exactly one match, or more than one match.

Upvotes: 2

Related Questions