0

I need to parse the string for the doubles it contains and store it in a list of doubles so later I can take the values and store them in a variable.

enter image description here

You can see on the picture in the richtextbox I have the string content now I tried to parse it like this

public void GetAll()
{       
   List<double> doubles = new List<double>();
   MatchCollection matches = Regex.Matches(buff, @"(([-]|[+])?\d+[.]\d+)");         
   foreach (Match match in matches)
   {
       string val = match.Groups[1].Value;
       doubles.Add(double.Parse(val));        
   }
}

and it doesnt store the doubles in the list.
I need them stored in order they show up doubles[0] = value of vv_in, doubles[1] = values of vv_out and so on.

12
  • 4
    Why not use double.TryParse instead of regex. If it returns true, you have a valid double in its out-parameter. Commented Jan 9, 2018 at 16:26
  • 1
    @HimBromBeere It looks like the OP needs to first parse the numeric part out of the string. Commented Jan 9, 2018 at 16:30
  • 2
    what is the content of buff ? is it a string containing the entire text from the textbox? Commented Jan 9, 2018 at 16:30
  • 1
    If it doesn't find the doubles, what does it do? Are there any matches for that regex on that string? What are they? What exactly is the string and can you parse it another way? Perhaps split by carriage returns, further split by whitespace, etc.? Commented Jan 9, 2018 at 16:30
  • 1
    OK, but you use a variable buff that we don't know how do you intialize it. If I prepare a string variable with a sample taken from your image I can parse it with your code correctly and the numbers appear in the list exactly in the order in which they are written on the test variable. What is the content of the buff variable when you call this method? (And why don't you pass it as parameter instead of using a global variable. I digress.) Commented Jan 9, 2018 at 16:55

4 Answers 4

4

You want to store the double that might be at the end of the strings? I'd use this approach:

List<double> doubles = new List<double>();
string[] lines = yourTextBox.Lines;
foreach(string line in lines)
{
    int lastSpaceIndex = line.LastIndexOf(' ');
    string doubleToken = line.Substring(++lastSpaceIndex);
    double d;
    if(double.TryParse(doubleToken, out d))
        doubles.Add(d);
}
Sign up to request clarification or add additional context in comments.

Comments

1

Can't really test it without your data, but this is worth a try:

var doubles = from line in buff.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries)
              from item in line.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
              where double.TryParse(item, out var _)
              select double.Parse(item);

As mentioned in the comments, this uses discards, which are a C# 7 feature.

1 Comment

string.Split splits at spaces by default, no need to provide ' ' to it.
0

Actually your code works almost fine. The only thing you have to tune, it that Parse needs to take the culture into account. And I changed your regex so it can cope with optional decimal digits:

List<double> doubles = new List<double>();

MatchCollection matches = Regex.Matches(buff, @"(([-]|[+])?\d+(.\d+)?)");  
foreach (Match match in matches)
{
    string val = match.Groups[1].Value;
    doubles.Add(double.Parse(val, System.Globalization.CultureInfo.InvariantCulture));

}

Comments

-1

You should first split your values into single lines and then every line at the whitespaces. At last try to parse the second part of the obtained array into a double:

public void GetAll()
{

   List<double> doubles = new List<double>();
   foreach(var line in buff.Split('\n')
   {
       var parts = line.Split();
       if(parts.Length == 3)
       {
           double d;
           if(double.TryParse(parts[2], out d)
               doubles.Add(d);
       }
    }
}

8 Comments

Yeah this wouldn't work given the input in the question, and thats assuming buff is even the textarea contents. I was not the downvoter however
@maccettura Indeed I missed a space. Apart from this: what else should buff be, as it obviously must contain a number
@HimBromBeere This still wouldn't work, at all. The OP's code assumes buff is the complete input from the text box. You're assuming a single number needs to be extracted from that. The OP attempts to extract all numbers from it.
@hvd I don´t see your point, where are more than one number in "VA W_IN 11.83"? Or you ment all lines?
@HimBromBeere hvd's point is that you are missing a loop to get every number
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.