从文本文件中解析字符串 [英] Parse string from Text file

查看:120
本文介绍了从文本文件中解析字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本文件,可以生成以下格式的文本。





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





我只想提取-0左边的11个字符,并将它们添加到List< string>中。因此,例如,如果该行是SEND BY C4DOC1 TO DC1713 A 17:11:46 3089 2731391571-0,则会将2731391571添加到列表< string> ;.



我有下面的代码,至少让我得到这些代码。



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).

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;
        }

推荐答案

我使用正则表达式:

I'd use a regex:
\w{10}(?=-0)

将仅提取-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)


然后你可以直接通过Linq生成一个列表:


You can then generate a list directly via Linq:

List<string> result = matches.Cast<Match>().Select(m => m.Value).ToList();





第一个正则表达式代码块消失[/ edit]



[edit]Code block vanished on first Regex[/edit]


这篇关于从文本文件中解析字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆