I would like to fetch some decimal numbers from a string which I receive from a remote service.

My issue is that I only want decimals in the string prefixed with either "+" or "-".

Here is my current solution:

```
string text = "+123.23 foo 456.34 bar -789.56";
List<string> decimals = Regex.Split(text, @"[^0-9\.]+").Where(
c => c != "." && c.Trim() != string.Empty).ToList();
foreach (var str in decimals)
{
Console.WriteLine(str);
}
// Output:
//
// 123.23
// 456.34
// 789.56
//
// Desired output:
//
// 123.23
// -789.56
```

Since I don't know regex well, I would like some help with a more suitable pattern.

I switched from `Split`

ting on non-numbers to `Match`

ing numbers. This gets the results you want:

```
string text = "+123.23 foo 456.34 bar -789.56";
List<string> decimals = Regex.Matches(text, @"[+\-][0-9]+(\.[0-9]+)?")
.Cast<Match>().Select(m => m.Value).ToList();
foreach (var str in decimals)
{
Console.WriteLine(decimal.Parse(str));
}
```

try `(\+|\-)[0-9\.]+`

```
string strRegex = @"(\+|\-)[0-9\.]+";
Regex myRegex = new Regex(strRegex);
string strTargetString = @"+123.23 foo 456.34 bar -789.56";
foreach (Match myMatch in myRegex.Matches(strTargetString))
{
if (myMatch.Success)
{
}
}
```

Similar Questions

Is there an easy way or integrated function to find out the decimal places of a floating point number? The number is parsed from a string, so one way is to count the digits after the '.' sign, but tha

I found the following code example on this blog post : final String FIBONACCI = (?x) .? | ( \\2?+ (\\1|^.) )* ..; for (int n = 0; n < 10000; n++) { String s = new String(new char[n]); if (s.matc

I need to find out whether a name starts with any of a list's prefixes and then remove it, like: if name[:2] in [i_, c_, m_, l_, d_, t_, e_, b_]: name = name[2:] The above only works

How can I find the files that have no numbers in the filename? find ~ -name '[a-z].*'

I've got a bunch of numbers that have multiple decimal points (so they're really strings). However, I want to sort them as if they were numbers. 1.1.1 10.2.3 2.6.7 21.10.4 3.10.12 4.11.5 4.1.16 6.4.

SetColoursSizes('0', '[Select Colour]', '0', '[Select Size]', 0,585.00,500.00); This is an example of the string I'd like to match in PHP, and then caputre the last two variables, in case, '585.00' a

I am a student in college and have an assignment which requires finding large prime numbers. I was given the following simple algorithm by the professor to find 2 likely prime numbers. generate ran

Part of my app has an area where users enter text into a textBox control. They will be entering both text AND numbers into the textBox. When the user pushes a button, the textBox outputs its text into

In PHP I have a list of country telephone codes. e.g: US 1, Egypt 20 .... I need to check if a given string starts with 00[ANY NUMBER FROM THE LIST]. How can I achieve this and return the country code

Hi what is the quickest and most efficient way of finding a string within another string. For instance i have this text; Hey @ronald and @tom where are we going this weekend However i want to find t

Looking to make a string that starts with either http:// or www clickable. str_replace(http://, $string, <a href='$string'>); str_replace(www, $string, <a href='$string'>); sh

I have a value returned as a number, with could be decimal number e.g. 1.15. However, I need to format all numbers in a range to a given fraction. For example, all numbers greater than 0 but less th

I have a string str ( 1 + 2 = 3 ). I want to obtain the individual numbers of the string in their decimal values( not ASCII ). I have tried atoi and c_str(). But both them require the entire string

This is the question. write a program that prompts the user to input five decimal numbers. the program should then add the five decimal numbers, convert the sum to the nearest integer,m and print the

Sorry in advance if this is a repeat question. I didn't see it listed elsewhere. I'm trying to find a regex string that will recognize a date of birth in the format of mmddyy. As far as I know though

It is not working if it is decimal value comparison. Please debug the code and provide me the solution. Please find the below code Thanks in Advance.. i=2.4524 j=2.430 s=0.2 sub=`echo $i - $j | bc`

i have value stored in string format & i want to convert into decimal. ex: i have 11.10 stored in string format when i try to convert into decimal it give me 11.1 instead of 11.10 . I tried it by

I have a form and I am using jquery validation plugin to validate it. Now I am trying to validate decimal number input. I have tried the following code, but its not working. Is the problem with the re

I am trying to move the following string -5017.85 into a decimal variable I've tried var sAmount = float.Parse(line[1]); sAmount = decimal.Parse(line[1]); sAmount = double.Parse(line[1]); sAmount =

I am a beginner in programmin in general and R specifically. I would like to generate a set of random numbers in a normal distribution but to limit the decimal places in these numbers to only 2. I hav

I was trying to create a calculator with java by using eclipse. I create this code here: package ozan; import java.util.Scanner; class apples { public static void main(String args[]) { Scanner ozan =

I read the JSON from a web service and converted all the JSON objects to HashMap<String, String> using GSON. I tested and my JSONObject it's fine (no decimal point in any of the numbers), but t

I have a case here where I am trying to figure out how a hex number is converted into a decimal number. I had a similar case before, but found out that if I reversed the hex string, and swapped each s

This question already has an answer here: Finding all numbers in a string 2 answers I have a string like: Hello I'm 43 years old, I need 2 burgers each for 1.99$. I need to parse it and get a

This question already has an answer here: JavaScript displaying a float to 2 decimal places 3 answers So I made this quick script that calculates some numbers, but the answer ends with many dec

I want to have a regexp to validate DECIMAL numbers: The valid DECIMAL numbers are : +123 123 -123 +123.0000 +123,123.999 .0234 +1123,233,34 invalid DECIMAL numbers are : +1233+ -1233_ +12.233.33 I

I have a problem with submitting forms when decimal numbers are present, to be precise if there is a number behind the decimal separator it wont submit. I am working with Asp.NET MVC3, Kendo UI and a

I have a sqlite table for my android application. This table has a String field (phone). When I want to insert or update a phone number starts with + plus-sign (e.g +905331234567) then it ignores th

I have an items table in my MySQL database with a DECIMAL(10,2) field called price. When fetching these values, is it safe to do calculations and such with these numbers in PHP? Or will I end up with

i need a regular expression for decimal/float numbers like 12 12.2 1236.32 123.333 and +12.00 or -12.00 or ...123.123... for using in javascript and jQuery. Thank you.

I have to do this exercise and am completely stumped as to how. I've constructed some FA's before, but using binary numbers. How can I do this but for even decimal numbers?

How to convert a binary String such as String c = 110010; // as binary to the value in decimal in Java? (expected result in the example is 50)

I am trying to get the total of my variables containing numbers, some may be decimals. I need this to be to two decimal places and am using the number_format() function. $total = $order->order->

I'm new to Java and trying to take a BigDecimal (for example 99999999.99) and convert it to a string but without the decimal place and trailing numbers. Also, I don't want commas in the number and rou

There are many inbulit functions like int(octal) which can be used to convert octal numbers into decimal numbers on command line but these doesn't work out in script . int(0671) returns 0671 in script

i have been given a string of numbers and have been tasked to reproduce only the numbers in the string with a funcion that prints within the function which i was able to do with the following. # func

I am trying to make a program that converts decimal numbers or text into binary numbers in Perl. The program asks for user input of a character or string and then prints out the result to the console.

I want to create a table to find missing sequence numbers. Sequence number between 0 to 70000 after reaching 70000 it becomes 0. In a particular period of time I need to find those missing records.

I have a number of decimal numbers stored in a database and need to calculate the percentage increase(or decrease) between two of the numbers using PHP. An example of two of the numbers are: 111.0516

I have a Hex String 40 that i would like to convert to its Decimal Base 10 value of 64. I tried: String^ val = 40; Decimal num = Convert::ToDecimal(val); but that doesn't work as i am clearly do

I am trying to create a jquery validation for number. It should allow 4 digit first and then after decimal point it should allow 1 digit. For decimal my code is working fine: $.validator.addMethod('De

I need to subtract numbers in my table and i'm using php to get the result e.g 17.45 - 7.15. The result im getting is a whole number. How can i get the difference of the two numbers with its decimal p

i have a string object String s = 64.5369474 British pounds what i want is to have a methos that can get the digits out and transfer into 2 decimal place, the result i expect to get is something l

This question already has an answer here: Finding a repeating sequence at the end of a sequence of numbers 5 answers I'm working with a list of lists that have the periods of continued fraction

How to add decimal point (with two decimal places) to string, without converting string to decimal? For example (Dim str1 as String), regardless str1 has value: 100 , 100.0 , 100.00, or 100.5 , 100.50

I have a property which has decimal datatype let's say Interest then I have another property of string type let's say InterestString. Properties public decimal Interest { get; set; } public stri

I Have this code to find out the total purchase price of some numbers and i need to know how to round the numbers to only 2 decimal places. #include <iostream.h> #include <cstdlib.h> #incl

I have an array of numbers that potentially have up to 8 decimal places and I need to find the smallest common number I can multiply them by so that they are all whole numbers. I need this so all the

How can I write a regular expression that validates an input text box that should contain only decimal values? The value can have at max 3 decimals (but also none) with comma as the separator. For ex

So after finally learning how to store user input into a variable, I am realizing it is rather difficult to have the user input numbers with decimals. The Number KeyPad doesn't allow a decimal part, a