Click here to Skip to main content
15,893,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a text file that produces text in the following format.


<RLOG>   <274,UN12,DC1713               51571618592>
(HLISL2-G)                 ENHANCED SEND LOG INQUIRY
  SEND BY C4DOC1 TO DC1713 A  17:11:46      3089    273XXX1571-0
    3107    272XXXX786-0                    3090    274ZZZ0005-0
  SEND DELIVERED TO RTEDI  AT 17:11:47      3091    264AAAA547-0
                                            3092    274BBB0003-0
  SEND BY HLAUTO TO DC1713 A  17:11:23      3093    274BBB0006-0
    3098    273XXXX259-0                    3094    272CCC1329-0
    3099    273XXX0706-0                    3095    274BVV0001-0
    3100    274XXX0008-0                    3096    274BWW0002-0
    3101    269XXX1385-0                    3097    273FDF3364-0


I just want to extract the 11 Chars to the left of the -0 and add them to a List<string>. So, for example, if the line is "SEND BY C4DOC1 TO DC1713 A 17:11:46 3089 2731391571-0" it would add "2731391571" to the list<string>.

I have the following code below which at least gets me the line(s).
C#
IList<string> result = new List<string>();
            string pattern = "-0";

            using (var reader = new StreamReader(@"C:\CBOT\RLOG.txt"))
            {
                string currentLine;
                //Match result=Regex.Match(line,@"^.*?(?=-0)");

                while ((currentLine = reader.ReadLine()) != null)
                {
                    if (currentLine.Contains(pattern))
                    {
                        string str = currentLine;
                        string ext = currentLine.Substring(0, str.IndexOf(pattern) + 1);
                        result.Add(ext);
                    }
                }
            }
            ListRLOG.ItemsSource = result;
        }
Posted
Updated 4-Oct-15 5:50am
v3
Comments
Richard MacCutchan 4-Oct-15 11:49am    
So, what is the question?
PIEBALDconsult 4-Oct-15 11:53am    
A Regular Expression ought to do it.

1 solution

I'd use a regex:
\w{10}(?=-0)
Will extract just the parts before the "-0", but if you want to extract just the ones that start "SEND " then try:
(?<=SEND\s.*)\w{10}(?=-0)

You can then generate a list directly via Linq:
C#
List<string> result = matches.Cast<Match>().Select(m => m.Value).ToList();


[edit]Code block vanished on first Regex[/edit]
 
Share this answer
 
v2
Comments
Maciej Los 4-Oct-15 16:02pm    
5ed!
BillWoodruff 4-Oct-15 22:44pm    
+5 ... mmm ... tasty !

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900