如何读取文件中的一行并在C#中操作文本 [英] How to read a line in a file and manipulate text in C#

查看:109
本文介绍了如何读取文件中的一行并在C#中操作文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好我从一个文本文件中读取了一行,但是在找到一个解决方案以消除最后8个字符来控制和只读取文本文件中的某些句子时遇到了麻烦。



例如我的文本文件说



SAMPLE DATA INFO
对齐值P0454穿制服> ;断路< caution>
将值P0334统一为>线宽<警告>
对齐值P0123均匀> BREAK SYSTEM< caution>
对齐值U0234穿制服>值数据<警告>
无关数据



我只想要信息P0454 P0334 P0123 U0234以及穿制服和小心打印到控制台之间的所有内容



所以我的输出应该是

 P0454断路器
P0334线路电线
P0123 BREAK系统
U0234价值数据





现在我有



< pre lang =c#> foreach string line in filetext)

if line.StartsWith( 对齐

Console.Writeline(line.substring( 20 ));





但这会读取数字加上我不想要的其他信息。如果可能,请欣赏任何想法。

解决方案

了解并使用正则表达式(参见正则表达式 [ ^ ] class)

看起来你想要一个像这样的模式:

 ^对齐值(\ S +)。*>(。+)< 



匹配行的两个捕获组可以提取到输出。

首先创建正则表达式

 使用 System.Text.RegularExpressions; 
private readonly static 正则表达式Extractor = new 正则表达式( @ ^对齐值( \ S +)。*>(。+)<,RegexOptions.Compiled);

这可以做一次并编译以提高效率。

然后处理这些行:

  foreach  string    filetext> 
{
匹配m = Extractor.Match(line);
if (m.Success)
{
Console.Writeline( {0} {1},m .Groups [ 1 ],m .Groups [< span class =code-digit> 2 ]);
}
}



(而且,,索引到群组是基于一个!)


我使用 Regex 和这样的命名捕获组。

  string  text =  @   示例数据信息   
< span class =code-string>对齐值P0454 uniformed> Circuit Break< caution > ;
将值P0334统一为> Line Wire< caution >
对齐值e P0123 uniformed> BREAK SYSTEM< caution >
对齐值U0234统一> VALUE DATA< caution < span class =code-string> >
UNRELATED DATA ;
正则表达式regex = 正则表达式(
^ \对齐值(?< ReferenceNo>。*?)。*?>(?< ;小心>。*?)<
RegexOptions.Multiline
| RegexOptions.CultureInvariant
| RegexOptions.Compiled
);

MatchCollection matches = regex.Matches(text);
foreach (匹配匹配 匹配)
{
控制台。 WriteLine(
参考:{0}警告:{1}
match.Groups [ ReferenceNo]。值,
match.Groups [ 警告]。值);
}


Hello I read a line from a text file and is having trouble coming up with a solution to eliminate the last 8 characters to console and reading only certain sentences within the text file.

For instance my text file says

" SAMPLE DATA INFO"
"Aligning the value P0454 uniformed>Circuit Break<caution">
"Aligning the value P0334 uniformed>Line Wire<caution">
"Aligning the value P0123 uniformed>BREAK SYSTEM<caution">
"Aligning the value U0234 uniformed>VALUE DATA<caution">
" UNRELATED DATA"


I just want the info P0454 P0334 P0123 U0234 and everything between uniformed and caution print to console

So my output should be

P0454 Circuit Break
P0334  Line Wire
P0123  BREAK SYSTEM
U0234  VALUE DATA



Right now I have

foreach(string line in filetext)

if line.StartsWith("Aligning")

Console.Writeline(line.substring(20));



But this reads the the number plus the rest of information which I don't want. Appreciate any ideas if possible.

解决方案

Learn about, and use, a regular expression (see the Regex[^] class)
It looks like you're going to want a pattern like:

^Aligning the value (\S+) .* >(.+)<


The two "capture groups" of matching lines can be extracted to the output.
First create the Regex:

using System.Text.RegularExpressions;
private readonly static Regex Extractor = new Regex(@"^Aligning the value (\S+) .* >(.+)<", RegexOptions.Compiled);

This can be done once and "compiled" for efficiency.
Then process the lines:

foreach(string line in filetext)
{
  Match m = Extractor.Match(line);
  if (m.Success)
  {
    Console.Writeline("{0} {1}", m.Groups[1], m.Groups[2]);
  }
}


(And, yes, the indexing into Groups is one-based!)


I'd use Regex with named capture groups like this.

 string text = @""" SAMPLE DATA INFO""
""Aligning the value P0454 uniformed>Circuit Break<caution"">
""Aligning the value P0334 uniformed>Line Wire<caution"">
""Aligning the value P0123 uniformed>BREAK SYSTEM<caution"">
""Aligning the value U0234 uniformed>VALUE DATA<caution"">
"" UNRELATED DATA""";
          Regex regex = new Regex(
     "^\"Aligning the value (?<ReferenceNo>.*?) .*?>(?<Caution>.*?)<",
   RegexOptions.Multiline
   | RegexOptions.CultureInvariant
   | RegexOptions.Compiled
   );
 
            MatchCollection matches = regex.Matches(text);
            foreach (Match match in matches)
            {
                Console.WriteLine(
                    "Reference: {0} Caution : {1}",
                    match.Groups["ReferenceNo"].Value,
                    match.Groups["Caution"].Value);
            }


这篇关于如何读取文件中的一行并在C#中操作文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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