locked
How to split string exactly with pattern matching case RRS feed

  • Question

  • Hi All, 

    I am facing bit problem to split string with pattern matching case here. I want to split string with pattern matching case only here. please help me here. 

    List<string> pattern = new List<string>();
                pattern.Add("[__]");
    
                string studentInfo = "[__] Student Attendance \n[__] Student Lunch Time\n(**) Parents Meeting Date \n[__] Student Play time\n[__] Student Library time, \n(**) Parents Observation \n";
    
                var finalStudentList = Regex.Split(studentInfo, string.Join("|", pattern.Select(c => Regex.Escape(@"" + c + "")).ToArray())).Where(s => s != string.Empty).ToArray<string>();
                string report = Environment.NewLine + string.Join(Environment.NewLine, finalStudentList);

    Here it's returning below string

    string report = "\r\n Student Attendance \n\r\n Student Lunch Time\n(**) Parents Meeting Date \n\r\n Student Play time\n\r\n Student Library time, \n(**) Parents Observation \n";

    but i don't want "(**) Parents Meeting Date \n" and "(**) Parents Observation \n"

    my output should be like below

    string report = "\r\n Student Attendance \n\r\n Student Lunch Time\n\r\n Student Play time\n\r\n Student Library time, \n"
    what should i need to do here. Please help me

    Thanks,
    Chandu


    Thursday, July 19, 2018 2:05 PM

Answers

  • That makes sense, but how will you know what to exclude and what to include? Is it only things with "[__]"? If so,

        var StudentList = Regex.Split(studentInfo, "\n").Where(s => !s.Contains(exclusionPattern) && s != string.Empty);
    

    to

        var StudentList = Regex.Split(studentInfo, "\n").Where(s => s.Contains(inclusionPattern) && s != string.Empty);
    

    If there is some other way to tell what to include or exclude you need to add appropriate logic in somewhere.

    Ethan


    Ethan Strauss

    Friday, July 20, 2018 2:55 PM

All replies

  • Your pattern in serving two purposes. It is determining the split and it is removing the funky characters ("[__]"). You also want to exclude some of the items. That needs to be done in another way. I suggest you approach it a bit differently and deal with the split in one way and removal of funky characters another way, 

                List<string> removalPattern = new List<string>();
                removalPattern.Add(@"[__]");
                removalPattern.Add(@"(**)");
                string exclusionPattern = "(**)";
                string studentInfo = "[__] Student Attendance \n[__] Student Lunch Time\n(**) Parents Meeting Date \n[__] Student Play time\n[__] Student Library time, \n(**) Parents Observation \n";
                var StudentList = Regex.Split(studentInfo, "\n").Where(s => !s.Contains(exclusionPattern) && s != string.Empty);
                var finalStudentList = StudentList.Select(s => s.Replace(removalPattern[0], "")).Select(s => s.Replace(removalPattern[1], ""));
                string report = Environment.NewLine + string.Join(Environment.NewLine, finalStudentList);

    This is clunky, but seems to work.

    Ethan


    Ethan Strauss

    Thursday, July 19, 2018 5:11 PM
  • Try this too:

    string report = string.Join( Environment.NewLine, Regex.Matches( studentInfo, @"(?<=\[__\]).+?(\n|$)" ).Cast<Match>().Select( m => m.Value ).Where( s => !string.IsNullOrWhiteSpace( s ) ) );


    • Edited by Viorel_MVP Thursday, July 19, 2018 7:17 PM
    Thursday, July 19, 2018 7:17 PM
  • Hi Ethan, 

    Thanks for u'r reply. But here "exclusionPattern" is not fixed. I will be getting multiple kind of patterns in string like it could be (...), [__], (/), etc. But here one pattern should be fixed, according to that i need to split the string. Remaining pattern matching string text i have to remove.  

    Friday, July 20, 2018 5:30 AM
  • That makes sense, but how will you know what to exclude and what to include? Is it only things with "[__]"? If so,

        var StudentList = Regex.Split(studentInfo, "\n").Where(s => !s.Contains(exclusionPattern) && s != string.Empty);
    

    to

        var StudentList = Regex.Split(studentInfo, "\n").Where(s => s.Contains(inclusionPattern) && s != string.Empty);
    

    If there is some other way to tell what to include or exclude you need to add appropriate logic in somewhere.

    Ethan


    Ethan Strauss

    Friday, July 20, 2018 2:55 PM