Answered by:
How to split string exactly with pattern matching case

Question
-
User321489315 posted
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:01 PM
Answers
-
User321489315 posted
Hi Kathy,
I found below solution. Thanks for u'r time :)
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 StudentList = Regex.Split(studentInfo, "\n").Where(s => s.Contains(pattern[0]) && s != string.Empty).ToArray();
Thanks,
Chandu
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, July 23, 2018 7:00 AM
All replies
-
User409696431 posted
You need to remove the (**) comments before splitting the string.
string input = @"[__] Student Attendance \n[__] Student Lunch Time\n(**) Parents Meeting Date \n[__] Student Play time\n[__] Student Library time, \n(**) Parents Observation \n"; string pattern = @"\\n\(\*\*\)[^\\]*"; string replacement = ""; string studentInfo = Regex.Replace(input, pattern, replacement);
And then continue with what you have
Friday, July 20, 2018 4:41 AM -
User321489315 posted
Hi Kathy,
Thanks for u'r reply. But here "(**)" is not fixed. I will be getting multiple kind of patterns in string like it could be (...), [__], (/), etc and it's dynamic. 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:24 AM -
User409696431 posted
I answered you based on the example you gave.
If you want to remove part of the string you need to have a fixed pattern to identify it. If you do, alter the Regex expression to match it. If you don't have a pattern that matches what you want to remove, you are out of luck.
Friday, July 20, 2018 6:41 AM -
User321489315 posted
Hi Kathy,
I found below solution. Thanks for u'r time :)
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 StudentList = Regex.Split(studentInfo, "\n").Where(s => s.Contains(pattern[0]) && s != string.Empty).ToArray();
Thanks,
Chandu
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, July 23, 2018 7:00 AM