C# 将文本分成三部分
我有一个带有YAML头的降价文件:C# 将文本分成三部分,c#,.net-core,C#,.net Core,我有一个带有YAML头的降价文件: --- title: Welcome post tags: [tech] enabled: true --- This is the post intro. *** This is the post body. 我正在使用以下命令读取文件: String text = File.ReadAllText(filePath); 如何从文本中获取文件的3个部分 --之间的YAML部分: title: Welcome post tags: [tech] en
---
title: Welcome post
tags: [tech]
enabled: true
---
This is the post intro.
***
This is the post body.
我正在使用以下命令读取文件:
String text = File.ReadAllText(filePath);
如何从文本中获取文件的3个部分
--
之间的YAML部分:
title: Welcome post
tags: [tech]
enabled: true
介绍部分,介于--
和***
之间:
This is the post intro.
This is the post body.
和正文部分,在***
之后:
This is the post intro.
This is the post body.
注意:有时没有介绍,所以只有YAML和Body:
---
title: Welcome post
tags: [tech]
enabled: true
---
This is the post body.
更新
我可以通过以下方法解决此问题:
String text = File.ReadAllText(file);
var result = text.Split(new String[] { "---", "***" }, StringSplitOptions.RemoveEmptyEntries);
不确定这是否是最好的方法,但它似乎有效。我将创建一个类来处理解析。大概是这样的:
class Program
{
static void Main(string[] args)
{
var yaml = new YamlParser("data.yaml");
Debug.WriteLine(string.Join(Environment.NewLine, yaml.Head));
// title: Welcome post
// tags: [tech]
// enabled: true
Debug.WriteLine(string.Join(Environment.NewLine, yaml.Intro));
// This is the post intro.
Debug.WriteLine(string.Join(Environment.NewLine, yaml.Body));
// This is the post body.
}
}
public enum YamlSection
{
Start,
Header,
Into,
Body,
Finished
}
public class YamlParser
{
public YamlParser(string file)
: this(System.IO.File.OpenText(file))
{ }
public YamlParser(StreamReader reader)
{
Head = new List<string>();
Intro = new List<string>();
Body = new List<string>();
var section = YamlSection.Start;
while (!reader.EndOfStream)
{
var line = reader.ReadLine();
if (string.IsNullOrWhiteSpace(line))
{
continue;
}
if (line.StartsWith("---") && section == YamlSection.Start)
{
section = YamlSection.Header;
}
else if (line.StartsWith("---") && section == YamlSection.Header)
{
section = YamlSection.Into;
}
else if (line.StartsWith("***"))
{
section = YamlSection.Body;
}
else
{
switch (section)
{
case YamlSection.Header:
Head.Add(line);
break;
case YamlSection.Into:
Intro.Add(line);
break;
case YamlSection.Body:
Body.Add(line);
break;
default:
throw new NotSupportedException();
}
}
}
section = YamlSection.Finished;
}
public List<string> Head { get; }
public List<string> Intro { get; }
public List<string> Body { get; }
}
类程序
{
静态void Main(字符串[]参数)
{
var yaml=新的YamlParser(“data.yaml”);
WriteLine(string.Join(Environment.NewLine,yaml.Head));
//标题:欢迎帖
//标签:[技术]
//已启用:true
Debug.WriteLine(string.Join(Environment.NewLine,yaml.Intro));
//这是文章的介绍。
WriteLine(string.Join(Environment.NewLine,yaml.Body));
//这是帖子的主体。
}
}
公共枚举分区
{
开始
标题,
进入
身体,
完成了
}
公共类YamlParser
{
公共YamlParser(字符串文件)
:this(System.IO.File.OpenText(文件))
{ }
公共YamlPasser(StreamReader)
{
Head=新列表();
Intro=新列表();
Body=新列表();
var段=YamlSection.Start;
而(!reader.EndOfStream)
{
var line=reader.ReadLine();
if(string.IsNullOrWhiteSpace(行))
{
继续;
}
if(line.StartsWith(“--”)和§ion==YamlSection.Start)
{
节=YamlSection.Header;
}
else if(line.StartsWith(“--”)和§ion==YamlSection.Header)
{
section=YamlSection.Into;
}
else if(第行开始,以“***”号填列)
{
截面=YamlSection.Body;
}
其他的
{
道岔(区段)
{
案例YamlSection.Header:
Head.Add(行);
打破
案例YamlSection.Into:
介绍添加(行);
打破
案例YamlSection.正文:
主体。添加(行);
打破
违约:
抛出新的NotSupportedException();
}
}
}
截面=YamlSection.Finished;
}
公共列表头{get;}
公共列表介绍{get;}
公共列表主体{get;}
}
您是否尝试过使用?不要阅读所有文本,请使用ReadAllLines或ReadLines阅读。然后,当您穿过屏幕时,查找您的“--”和“***”行file@EdPlunkett是的,我正在使用它来解析YAML文本,但是我无法使用YamlDotNet获得其他两部分。这就是为什么我希望先分成3部分,然后做我需要的任何事情。