How to parse data from C# string in a ListBox or directly from the returning string recieved from the Serial Port

I need to extract the numbers from the string in the code I shall attach, but the trick will be that there may be one, or up to about 5 possible items returned. I only want to extract the 6 digit numbers contained in the string.

Here is a string that contains an example. Each value is comma separated so it should be easy, I just don't have a clue how to go about doing it. The example has two numbers to extract.

at+cops=?
COPS cmd may take 1 min for resp.
+COPS: (2,"My Network","","310410"),(0,"My Network","","310260")  //Need to extract these numbers from a string like this one.


private void timer2_Tick(object sender, EventArgs e)
        {
            _sp.ReadTimeout = 120000;
            List<string> response = new List<string>();
            response = SendCommand("at+cops=?", 4);
            foreach (string line in response)
            {
                mainListBox.Items.Insert(0, line);
                if (line.Contains("+COPS: "))  //here is where I identify the line with the information
                {
                     //Ineed code here to extract numbers to an array
                }
                        else
                        {
                            continue;
                        }
            }

Open in new window

tazz500Asked:
Who is Participating?
 
Sreedhar VengalaConnect With a Mentor Sr. Consultant - Business IntelligenceCommented:
Hello Tazz500
Someway on these line would might help you:
List<string> response = new List<string>();
 
            //+COPS: (2,"My Network","","310410"),(0,"My Network","","310260")  
 
            string fullText =       ("+COPS: (2," + @"""My Network""" + "," + @"""""" + "," + @"""310410""" + "),") 
                            +          ("(0," + @"""My Network""" + "," + @"""""" + "," + @"""310260""" + "), ")
                            +          ("(0," + @"""My Network""" + "," + @"""""" + "," + @"""420421""" + ") ");
 
            string[] fullItem = fullText.Split(')');
            var extractedValues = new List<int>();
            foreach (var itemString in fullItem)
            {
                string[] innerItem = itemString.Split(',');
                foreach (var innerItemString in innerItem)
                {
                    if(innerItemString.ToCharArray().Length == 8)
                    {
                        string[] strings1 = innerItemString.Split('"');
                        extractedValues.Add(Convert.ToInt32(strings1[1]));
 
                    }
                }
 
              
            }

Open in new window

0
 
BalkisBrConnect With a Mentor Commented:
Hi,

you can use this code with regex, feel free to improve it
string[] array = Regex.Split(line, "([\"]\\d+[\"])");            
            
List<int> FinalList = new List<int>();
foreach (string s in array)
    if (Regex.IsMatch(s, "([\"]\\d+[\"])"))
        FinalList.Add(Convert.ToInt32(s.Replace("\"",""))); 

Open in new window

0
 
tazz500Author Commented:
Thanks for the help! Between some thing you said and another source I came up with this....
private void timer2_Tick(object sender, EventArgs e)
        {
            _sp.ReadTimeout = 120000;
            List<string> response = new List<string>();
            response = SendCommand("at+cops=?", 4);
            foreach (string line in response)
            {
                mainListBox.Items.Insert(0, line);
                Regex regex = new Regex(@"^\+COPS: (\(\d+,""[^""]*"",""[^""]*"",""(\d+)""\),?)+\s*$");
                Match match = regex.Match(line);
                if (match.Success)
                {
                    List<string> matches = new List<string>();
                    foreach (Capture capture in match.Groups[2].Captures)
                    {
                        matches.Add(capture.Value);
                    }
 
                    foreach (string providers in matches)
                    {
                    comboBox1.Items.Add(providers);
                    }                     
                 
              }
 
            }             

Open in new window

0
 
BalkisBrCommented:
Great, good solution!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.