首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >C#:解析文本文件

C#:解析文本文件
EN

Stack Overflow用户
提问于 2014-05-05 06:00:12
回答 4查看 229关注 0票数 0

我有文本文件,文件的内容是这样的:

代码语言:javascript
复制
idiom: meaning
description.
o example1.
o example2.

idiom: meaning
description.
o example1.
o example2.

.
.
.

如您所见,该文件包含上述段落,每个段落都有一些我希望提取的数据(请注意,示例以o开头)。例如,我们有以下数据:

代码语言:javascript
复制
public class Idiom
{
    public string Idiom { get; set; }
    public string Meaning { get; set; }
    public string Description { get; set; }
    public IList<IdiomExample> IdiomExamples { get; set; }
}

public class IdiomExample
{
    public string Item { get; set; }
}

有办法提取文件中的那些字段吗?知道吗?

编辑的

那个文件可以是任何东西,像成语和动词,.例如,这就是我的模式--例如:

代码语言:javascript
复制
little by little: gradually, slowly (also: step by step)
o Karen's health seems to be improving little by little.
o If you study regularly each day, step by step your vocabulary will increase.
to tire out: to make very weary due to difficult conditions or hard effort (also: to wear out) (S)
o The hot weather tired out the runners in the marathon.
o Does studying for final exams wear you out? It makes me feel worn out!

预先感谢

EN

Stack Overflow用户

发布于 2014-05-05 07:23:41

您的示例没有描述,但是这个regexp接受可选的描述。它让您了解如何解析输入,而不是整个C#代码。

请看这里的这个演示,看看这些组

代码语言:javascript
复制
(?smx)
^ 
([^:\n]+):\s*([^\n]+)
\n([^o].*?\n|)
(^o.*?)
(?=\Z|^[^o:\n]+:)

在此之后:

  1. Group#1有成语
  2. Group#2有意义
  3. 如果有,Group#3有描述
  4. Group#4提供了所有示例

此正则表达式不会将示例解析为几个示例,这是下一个工作。另外,你可能不喜欢一些新词。

票数 1
EN
查看全部 4 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23465773

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档