Regex Ruby:解析一个简单的标记文件(具有相似但不相等的结构)并将内容填充到对象';s属性
我有一个满是降价文件的文件夹。我想将它们中的每一个读入以下Ruby对象:Regex Ruby:解析一个简单的标记文件(具有相似但不相等的结构)并将内容填充到对象';s属性,regex,ruby,parsing,markdown,Regex,Ruby,Parsing,Markdown,我有一个满是降价文件的文件夹。我想将它们中的每一个读入以下Ruby对象: File: h1: "This is the title" description: "This is some description.\n\nAnd even more description." content: "## This is an h2...etc. etc." File: h1: nil description: nil
File:
h1: "This is the title"
description: "This is some description.\n\nAnd even more description."
content: "## This is an h2...etc. etc."
File:
h1: nil
description: nil
content: "## This is an h2...More bla bla."
File:
h1: "This is the title"
description: nil
content: "## This is an h2...Bla bla.
File:
h1: nil
description: This is a description...Some more description.
content: "## This is an h2...Bla bla.
类文件
属性访问器:标题、描述、内容
结束
标记文件通常如下所示:
# This is the title
This is some description.
And even more description.
## This is an h2
Bla bla.
## This is another h2
More bla bla.
### This is even an h3
Again, more bla bla.
## Again, an h2
etc. etc.
这将导致以下Ruby对象:
File:
h1: "This is the title"
description: "This is some description.\n\nAnd even more description."
content: "## This is an h2...etc. etc."
File:
h1: nil
description: nil
content: "## This is an h2...More bla bla."
File:
h1: "This is the title"
description: nil
content: "## This is an h2...Bla bla.
File:
h1: nil
description: This is a description...Some more description.
content: "## This is an h2...Bla bla.
为了将文件的内容分配给Ruby对象的定义,我可以简单地使用一个正则表达式,它将提取title
(第一个H1)、description
(H1和下面的H2之间的文本)和content
(所有其他内容)
但文件并不总是像这样:
# This is the title
This is some description.
And even more description.
## This is an h2
Bla bla.
## This is another h2
More bla bla.
### This is even an h3
Again, more bla bla.
## Again, an h2
etc. etc.
- 有时,没有H1
- (如果是,文件名将用于
)标题
- (如果是,文件名将用于
- 有时,没有描述
- 有时,没有内容
## This is an h2
Bla bla.
## This is another h2
More bla bla.
# This is the title
## This is an h2
Bla bla.
This is a description.
Some more description.
## This is an h2
Bla bla.
这将导致以下Ruby对象:
File:
h1: "This is the title"
description: "This is some description.\n\nAnd even more description."
content: "## This is an h2...etc. etc."
File:
h1: nil
description: nil
content: "## This is an h2...More bla bla."
File:
h1: "This is the title"
description: nil
content: "## This is an h2...Bla bla.
File:
h1: nil
description: This is a description...Some more description.
content: "## This is an h2...Bla bla.
或包含H1但没有说明的文件:
## This is an h2
Bla bla.
## This is another h2
More bla bla.
# This is the title
## This is an h2
Bla bla.
This is a description.
Some more description.
## This is an h2
Bla bla.
这将导致以下Ruby对象:
File:
h1: "This is the title"
description: "This is some description.\n\nAnd even more description."
content: "## This is an h2...etc. etc."
File:
h1: nil
description: nil
content: "## This is an h2...More bla bla."
File:
h1: "This is the title"
description: nil
content: "## This is an h2...Bla bla.
File:
h1: nil
description: This is a description...Some more description.
content: "## This is an h2...Bla bla.
或没有H1但有描述的文件:
## This is an h2
Bla bla.
## This is another h2
More bla bla.
# This is the title
## This is an h2
Bla bla.
This is a description.
Some more description.
## This is an h2
Bla bla.
这将导致以下Ruby对象:
File:
h1: "This is the title"
description: "This is some description.\n\nAnd even more description."
content: "## This is an h2...etc. etc."
File:
h1: nil
description: nil
content: "## This is an h2...More bla bla."
File:
h1: "This is the title"
description: nil
content: "## This is an h2...Bla bla.
File:
h1: nil
description: This is a description...Some more description.
content: "## This is an h2...Bla bla.
我想知道我是否可以使用一个奇特的正则表达式(我不是这方面的专家),或者我是否应该尝试以某种方式将其拆分为几个过程步骤。我在这里问了一个类似的问题:,但是除了上面描述的异常,我无法使用Ruby使正则表达式正常运行
任何解决这个问题的想法都是非常受欢迎的。多谢各位
PS:我还考虑过使用标记解析器解析标记,然后使用Nokogiri或其他允许我解析结果的东西。但对于这样一个基本上很简单的需求来说,这感觉开销太大了。举个例子:
examples=[]
示例所有这些不同的示例都让我有点困惑,输入可能是什么样子的。这些示例的预期输出是什么?可能还想重新思考一下您的类名,我可以自由地在您的帖子中添加更多描述性细节:在每个示例之后,现在都有一个关于相应Ruby对象外观的描述。我真的不明白你想在do中实现什么。。。结束
block,但应该有类似于File.new的内容(h1:h1,description:description,content:content)
。也许你可以相应地修改代码?谢谢。哦,在我的回答中为你的问题添加了细节,你说得对,我应该把这些细节加入到问题中。我现在就移动它们。更新了答案以符合规格。亲爱的,我们现在非常接近了。我刚刚添加了另一个我忘记的例子:或者一个没有H1的文件,但是有一个描述。如果你能更新你的正则表达式,那么我想我们完成了!✅