RegEx in c# for a piped (
I have an multiline input string of following format
ID | Col1 | Col2 | Col3 | Col4 | Col5 | Col6 | Col7 | Col8
The data might run into thousands of rows Data Type for Col1-8 could be alphanumeric or alpha only which may differ from row to row.
I need to do following:
Extract Col2 & Col3 out (Thinking of using Regex here instead of usual string splitting since later would be slower in my case?)
I want the rest of the data also in following format also. ID | Col1 | Col4 | Col5 | Col6 | Col7 | Col8 (This I can achieve using string.Replace once I have the data in point 1)
Please note that I do not want to loose data of Col2 & Col3 while achieving solution for point 2 and hence the first point is also important to me.
I tried creating RegEx for point 1 as (|){2,4} which I understand now is completely wrong and curently I have no idea how to proceed on this using Regex.
I would be thankful for any help/pointers on how to go about creating a regex for same.
有没有理由不能这样做?
var str = "ID | Col1 | Col2 | Col3 | Col4 | Col5 | Col6 | Col7 | Col8";
var strA = str.Split(" | ".ToArray(), StringSplitOptions.RemoveEmptyEntries).ToList();
var strExtract = new List<string> { strA[2], strA[4] };
strA.RemoveAt(2);
strA.RemoveAt(3);
Console.WriteLine(string.Join(" | ", strA.ToArray()));
var cols = input.Split(new[] {'|'}, StringSplitOptions.RemoveEmptyEntries)
.Select(s => s.Trim())
.ToList();
var extractedcols = cols.Skip(2).Take(2);
var output = string.Join(" | ", cols.Except(extractedcols));
使用.Split'|',5)将前4个元素和其余的刺作为一个数组。
链接地址: http://www.djcxy.com/p/68828.html上一篇: 选择数据表的某些列