[C#] Collecting part of output stream and putting it into a list

Discussion in 'Mixed Languages' started by CODYQX4, Dec 10, 2010.

  1. CODYQX4

    CODYQX4 MDL Developer

    Sep 4, 2009
    4,804
    45,136
    150
    #1 CODYQX4, Dec 10, 2010
    Last edited by a moderator: Apr 20, 2017
    OK I am redirecting input and output to an old beta tool called OSPPRUN. It connects to OSPPSVC and tests some functions that I'm using.

    I dumped it's output but only want part of it.

    Code:
    > 
    OK.
    
    >    0. 2453acfb-0e3f-49b2-9a78-209b1ff3908c
       1. 7bcc5eee-f730-4c93-97c4-877e27333324
       2. 82bb1ddf-033e-4de5-a65f-88465f74aecf
       3. e2e326c2-ca6a-4577-a305-9d1e03fc5773
    
    OK.
    
    >    0. 1ddfa3a5-e89b-44aa-b58a-1a007c00c6b4
       1. 4d9f9ff4-b7f5-452b-9587-8d2b38dd0c45
       2. 5d77cb5d-6945-497a-a568-d01ffa6d7c80
       3. 64026624-f349-46f0-87db-37442cf6f3da
       4. 70202bca-2321-4f18-a3ff-29cd3ba38307
       5. 98aa5ac0-0173-4df5-a223-670bb3bdef16
       6. d8f1b81d-bca4-4879-aea8-3845a14c4824
    
    OK.
    
    > 
    Error code hr = 0xC004F012,The Software Licensing Service reported that the call has failed because the value for the input key was not found.
    
    
    
    > 
    OK.
    
    > Bye-Bye
    
    If you see the 0., 1, etc lines, I only want those and I want the "1. " removed from it and put into a string list. So it should get all those codes as I put them into a list and pass them to another function and collect them in a second list, then I pass those to a function and it will uninstall the Licenses.

    I'm using a streamreader to hold all that output in the code blocks.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  2. Calistoga

    Calistoga MDL Senior Member

    Jul 25, 2009
    420
    198
    10
    #2 Calistoga, Dec 10, 2010
    Last edited by a moderator: Apr 20, 2017
    Does this work? I wrote the code directly into this post, so I may have made errors ;)

    Code:
    string pattern = @"(?<Number>\d+?)\.\s+?(?<License>(?:\d|\w){8}-(?:(?:\d|\w){4}-){3}(?:\d|\w){12})";
    
    Regex regex = new Regex(pattern);
    
    MatchCollection matches = regex.Matches(yourStringWithData);
    
    // This can be changed to <int, string> if you want to convert the number into an integer type with int.Parse() or int.TryParse().
    Dictionary<string, string> licenses = new Dictionary<string, string>();
    
    foreach (Match match in matches)
    {
        string number = match.Groups["Number"].Value;
        string license = match.Groups["License"].Value;
    
        // Use the matched strings
        licenses.Add(number, license);
    }
    
    // All licenses can now be accessed from the dictionary
    
     
  3. CODYQX4

    CODYQX4 MDL Developer

    Sep 4, 2009
    4,804
    45,136
    150
    #3 CODYQX4, Dec 10, 2010
    Last edited: Dec 10, 2010
    (OP)
    I can't seem to tell if it works as it freezes so I can't tell what happens. It does compile.

    Keep in mind I do not ever want to use the "0. " part, only the 1ddfa3a5-e89b-44aa-b58a-1a007c00c6b4, and those listed are just a few of the possibilities. So storing it is unneeded, it needs to be removed and the 1ddfa3a5-e89b-44aa-b58a-1a007c00c6b4 part should have no spaces in front of it.

    EDIT: It works, I had to change the way things were working in other code as it was freezing for some reason and now I fixed it.

    Thanks for the help. A License Uninstaller in toolkit V2.1 greatly depended on this.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  4. Calistoga

    Calistoga MDL Senior Member

    Jul 25, 2009
    420
    198
    10
    #4 Calistoga, Dec 11, 2010
    Last edited by a moderator: Apr 20, 2017
    Glad you got it working :) This is a revised version that disregards the 0.'s - just posting it for future reference

    Code:
    using System.Collections.Generic;
    using System.Text.RegularExpressions;
    
    string pattern = @"(?:\d|\w){8}-(?:(?:\d|\w){4}-){3}(?:\d|\w){12}";
    
    Regex regex = new Regex(pattern);
    
    MatchCollection matches = regex.Matches(yourStringWithData);
    
    List<string> licenses = new List<string>();
    
    foreach (Match match in matches)
    {
        string license = match.Value.ToLowerInvariant().Trim(); // For casing consistency, and removes eventual spaces
    
        licenses.Add(license);
    }