william007
william007

Reputation: 18547

Extract digit in a string

I have a list of string

goal0=1234.4334abc12423423
goal1=-234234
asdfsdf

I want to extract the number part from string that start with goal, in the above case is

1234.4334, -234234

(if two fragments of digit get the first one) how should i do it easily?

Note that "goal0=" is part of the string, goal0 is not a variable. Therefore I would like to have the first digit fragment that come after "=".

Upvotes: 1

Views: 377

Answers (8)

jomsk1e
jomsk1e

Reputation: 3625

Please try this:

string sample = "goal0=1234.4334abc12423423goal1=-234234asdfsdf";

Regex test = new Regex(@"(?<=\=)\-?\d*(\.\d*)?", RegexOptions.Singleline);
MatchCollection matchlist = test.Matches(sample);

string[] result = new string[matchlist.Count];
if (matchlist.Count > 0)
{
    for (int i = 0; i < matchlist.Count; i++)
    result[i] = matchlist[i].Value;
}

Hope it helps.

I didn't get the question at first. Sorry, but it works now.

Upvotes: 2

Tobsey
Tobsey

Reputation: 3400

List<String> list = new List<String>();

list.Add("goal0=1234.4334abc12423423");
list.Add("goal1=-23423");
list.Add("asdfsdf");

Regex regex = new Regex(@"^goal\d+=(?<GoalNumber>-?\d+\.?\d+)");

foreach (string s in list)
{
    if(regex.IsMatch(s))
    {
        string numberPart = regex.Match(s).Groups["GoalNumber"];

        // do something with numberPart
    }
}

Upvotes: 0

didster
didster

Reputation: 882

Try this

string s = "goal0=-1234.4334abc12423423";
string matches =  Regex.Match(s, @"(?<=^goal\d+=)-?\d+(\.\d+)?").Value;

The regex says

  • (?<=^goal\d+=) - A positive look behind which means look back and make sure goal(1 or more number)= is at the start of the string, but dont make it part of the match
  • -? - A minus sign which is optional (the ? means 1 or more)
  • \d+ - One or more digits
  • (\.\d+)? - A decimal point followed by 1 or more digits which is optional

This will work if your string contains multiple decimal points as well as it will only take the first set of numbers after the first decimal point if there are any.

Upvotes: 3

Nikola Davidovic
Nikola Davidovic

Reputation: 8666

You can do the following:

        string input = "goal0=1234.4334abc12423423";
        input = input.Substring(input.IndexOf('=') + 1);

        IEnumerable<char> stringQuery2 = input.TakeWhile(c => Char.IsDigit(c) || c=='.' || c=='-');
        string result = string.Empty;
        foreach (char c in stringQuery2)
            result += c;
        double dResult = double.Parse(result);

Upvotes: 4

lex87
lex87

Reputation: 1336

string s = "1234.4334abc12423423";
var result = System.Text.RegularExpressions.Regex.Match(s, @"-?\d+");

Upvotes: 0

Joel Coehoorn
Joel Coehoorn

Reputation: 416131

You can use the old VB Val() function from C#. That will extract a number from the front of a string, and it's already available in the framework:

result0 = Microsoft.VisualBasic.Conversion.Val(goal0);
result1 = Microsoft.VisualBasic.Conversion.Val(goal1);

Upvotes: 1

Vintage
Vintage

Reputation: 238

Use a regex for extracting:

x = Regex.Match(string, @"\d+").Value;

Now convert the resulting string to the number by using:

finalNumber = Int32.Parse(x);

Upvotes: 2

Randy Minder
Randy Minder

Reputation: 48522

I think this simple expression should work:

Regex.Match(string, @"\d+")

Upvotes: 1

Related Questions