-3

there is a file which i want to split

MSH|^~\&||||^asdasdasd|||asdasd|637226866166648574|637226866166648574|2.4
EVN|asd|20200416|20200416
PID|1|PW9074asdasd41|asd|PW907441|asdsad^wqe^wqeqwe||19700524|M
MSH|^~\&||||^qweqwewqe|||qwewqeqw|637226866166648574|637226866166648574|2.4
EVN|P03|20200416|20200416
PID|1|PW907441|PW907441|PW907441|Purvis^Walter^Rayshawn||19700524|M

I want to split it using MSH so that the result would be an array of string

array[0]=
"MSH|^~\&||||^asdasdasd|||asdasd|637226866166648574|637226866166648574|2.4
EVN|asd|20200416|20200416
PID|1|PW9074asdasd41|asd|PW907441|asdsad^wqe^wqeqwe||19700524|M";

array[1]=
"MSH|^~\&||||^asdasdasd|||asdasd|637226866166648574|637226866166648574|2.4
EVN|asd|20200416|20200416
PID|1|PW9074asdasd41|asd|PW907441|asdsad^wqe^wqeqwe||19700524|M";

What I have tried so far:

string[] sentences = Regex.Split(a, @"\W*((?i)MSH(?-i))\W*");

result:

array[0]="";
array[1]="MSH";
array[2]="asdasdasd|||asdasd|637226866166648574|637226866166648574|2.4
    EVN|asd|20200416|20200416
    PID|1|PW9074asdasd41|asd|PW907441|asdsad^wqe^wqeqwe||19700524|M";
array[3]="MSH";
array[4]="asdasdasd|||asdasd|637226866166648574|637226866166648574|2.4
    EVN|asd|20200416|20200416
    PID|1|PW9074asdasd41|asd|PW907441|asdsad^wqe^wqeqwe||19700524|M";

Or atleast it should not miss |^~\&||||^ after split in index 1 and 2

ccaring
  • 53
  • 7

2 Answers2

-1

You can simply use the Split() function for this. Below generates an IEnumerable, which you can make an array using ToArray if you wanted to:

void Main()
{
    string s = @"MSH|^~\&||||^asdasdasd|||asdasd|637226866166648574|637226866166648574|2.4
EVN|asd|20200416|20200416
PID|1|PW9074asdasd41|asd|PW907441|asdsad^wqe^wqeqwe||19700524|M
MSH|^~\&||||^qweqwewqe|||qwewqeqw|637226866166648574|637226866166648574|2.4
EVN|P03|20200416|20200416
PID|1|PW907441|PW907441|PW907441|Purvis^Walter^Rayshawn||19700524|M";

    foreach (var element in s.Split(new string[] { "MSH" }, StringSplitOptions.RemoveEmptyEntries).Select(x => $"MSH{x}"))
    {
        Console.WriteLine(element);
    }
}
Cetin Basoz
  • 22,495
  • 3
  • 31
  • 39
  • On the contrary. Your solution misses the fact OP used a case insensitive `MSH` string to split. Your solution won't work if there is `Msh` or `mSh`, so it is wrong. – Wiktor Stribiżew May 06 '20 at 09:52
  • 1
    @WiktorStribiżew and you simply missed the fact that it was later added to requirements. There is no way to know what is in one's mind beforehand. We don't have magic 8 ball here. – Cetin Basoz May 06 '20 at 09:53
  • No, it was not later added, it was there when you posted the answer. Do you understand what `(?i)` means? That is exactly why OP used a regex in the first place. – Wiktor Stribiżew May 06 '20 at 09:53
  • @WiktorStribiżew I see your math is a bit lacking. Check the timings. – Cetin Basoz May 06 '20 at 09:54
  • I have been here all the time, no math required. Please fix your answer or remove. – Wiktor Stribiżew May 06 '20 at 09:54
  • No I am not going to remove it. Nobody on SO continuously scans for the OP questions' changes in time to update their answers. My answer was perfectly valid and correct at the point of time as the question stands. Later it was added that it could be case insensitive. If you have asked if that should be at a word boundary and or new line then he would probably say yes, word boundary, new line etc and you would start to add \b ... There is no end to such cases. You are free to down vote. If you could simply check the timings you would see my answer was before any edits. – Cetin Basoz May 06 '20 at 09:57
  • No. If you like you can vote to close the question instead. What is your problem with the "correct" answer. It is your sole opinion that it is irrelevant. – Cetin Basoz May 06 '20 at 09:59
-1

If you want to split on MSH, Cetin Basoz is right. It will perfectly work doing that :

var sentences = a.Split(new String[] { "MSH" }, StringSplitOptions.RemoveEmptyEntries);

If you wanna be case insensitive, you can use that which is much simpler than the regex you used previously :

var sentences = Regex.Split(a, "MSH", RegexOptions.IgnoreCase);
Kirjava
  • 226
  • 1
  • 11