locked
Convert matchcollection group value to list using linq in c# RRS feed

  • Question

  • User1140724383 posted

    Hi friends,

    I have one task

    I need to populate the List from Match Collection values

    these values of List should be separated with comma

    I can't in this last point (these values of List should be separated with comma)

    on the List the return all Match Collection values are not separated

    it seems that the string.Join does not work

    can you help me, please?

    My code below

                                MatchCollection matches = re.Matches(s.ToString());
    
                                if (matches.Count > 0)
                                {
                                    var result = matches.Cast<Match>().Select(m => m.Value.Trim()).ToArray();
    
                                    foreach (var item in result)
                                    {
                                        Response.Write(string.Join(",", item));
                                    }
                                }

    Thursday, May 14, 2020 8:08 PM

Answers

  • User288213138 posted

    Hi mimidu,

    Please try to clear the cache in your browser, and then run your code.

    If you still can't solve the problem, please post all your code.

    Best regards,

    Sam

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 18, 2020 1:48 AM

All replies

  • User475983607 posted

    Try...

    MatchCollection matches = re.Matches(s.ToString());
    if (matches.Count > 0)
    {
       var result = matches.Cast<Match>().Select(m => m.Value.Trim()).ToArray();
       Response.Write(string.Join(",", result));
    }

    Thursday, May 14, 2020 8:43 PM
  • User1140724383 posted

    thank you

    I have tried your suggestion, but the result not have changed

    site1site2site3site4

    Instead of

    site1, site2, site3, site4

    Friday, May 15, 2020 6:43 AM
  • User475983607 posted

    mimidu

    thank you

    I have tried your suggestion, but the result not have changed

    site1site2site3site4

    Instead of

    site1, site2, site3, site4

    You are doing somehting wrong.  Probably related to the Response.Write.  Please run your code through the Visual Studio debugger.

    Friday, May 15, 2020 9:52 AM
  • User1140724383 posted

    ok

    on Visual Studio debugger the result don't have changed

    I tried with

      IEnumerable<Match> combined = matches.OfType<Match>().Concat(matches.OfType<Match>()).Where(m => m.Success);
      Response.Write(string.Join(", ", combined) + "<br />");

    in this case the result

    site1, site1
    site2, site2
    site3, site3
    site4, site4

    instead of

    site1,
    site2,
    site3,
    site4

    Any suggestion?

    Friday, May 15, 2020 10:29 AM
  • User475983607 posted

    So the comma magically showed up?  Anyway, Concat is probably causing the problem.  Please debug your code!

    Friday, May 15, 2020 11:31 AM
  • User1140724383 posted

    So the comma magically showed up?  Anyway, Concat is probably causing the problem.  Please debug your code!

    this is the result on debug code

    site1, site1
    site2, site2
    site3, site3
    site4, site4

    In visual studio 2019 > Debug > F5

    Friday, May 15, 2020 11:36 AM
  • User288213138 posted

    Hi mimidu,

    Please try to clear the cache in your browser, and then run your code.

    If you still can't solve the problem, please post all your code.

    Best regards,

    Sam

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 18, 2020 1:48 AM