Ruby 如何正则化两组数据
我的文本包含对我的数据的描述,并以两组数据结尾:Ruby 如何正则化两组数据,ruby,regex,parsing,Ruby,Regex,Parsing,我的文本包含对我的数据的描述,并以两组数据结尾: LONG TEXT - TAGS:(Array of TAGS) - URL 我需要解析这个文本并构造我的数据 我的想法是要有这样的东西: a="LONG TEXT. Tags:[ex1,ex2,ex3]" 然后使用a=a.partition(“Tags:”)(partition分割arg上的文本),这样我就可以在a[0]上得到长文本,在a[2]上得到标记,然后我将它们分割成所有单独的标记。如果“标记:在长文本中,这可能是一个问题,但我可以使
LONG TEXT - TAGS:(Array of TAGS) - URL
我需要解析这个文本并构造我的数据
我的想法是要有这样的东西:
a="LONG TEXT. Tags:[ex1,ex2,ex3]"
然后使用a=a.partition(“Tags:”)
(partition
分割arg上的文本),这样我就可以在a[0]
上得到长文本,在a[2]
上得到标记,然后我将它们分割成所有单独的标记。如果“标记:
在长文本中,这可能是一个问题,但我可以使标记:
唯一以使其工作
我在如何存储URL方面遇到了问题。我猜在URL上对字符串进行分区也可以,但我相信在正则表达式中有一种更好的方法来实现这一点,它更适合于数据。我应该如何构造我的数据,使我能够在它们存储在文本中时检索它们
更新:我可以告诉我的用户以某种方式构造他们的数据,但这需要很简单。对于输出,我只需要文本、字符串数组和url
例如,我可以告诉我的用户输入:
"This is a long text, which is the original description which I have to get and
store as text in my database.
TAGS:[Tag1,Tag 2]
URL:http://google.com"
或者我可以告诉他们将其构造为YAML格式。但我的目标是简单
如果说我使用上面的输入,那么最好的方法是什么
"This is a long text, which is the original description which I have to get and
store as text in my database."
array of "Tag1", "Tag 2"
"http://google.com"
TL;博士
在您的情况下,最好的做法是为您的用户提供一个简单(但评论良好)的模板,供他们编辑,并将其粘贴到自由格式的文本字段中。YAML非常用户友好,可以手动编辑。然后可以将YAML轻松解析为一个可在应用程序中使用的文件
YAML模板示例
以下是YAML标记的基本示例,其中描述、标记和url定义为哈希键。您可以看到,只要用户注意连续行的缩进,修改是多么容易
:description: This is a long text, which is the original description
which I have to get and store as text in my database.
:tags:
- tag1
- tag2
- tag3
:url: http://example.com
请注意,标记的数组数据是缩进的,以便于阅读。然而,如果在这种特殊情况下标记没有缩进,Ruby的YAML解析器也会很高兴
您还可以自由地使用文本模板中的注释来提供YAML格式帮助,或者记录有效的键或值。例如:
# Make sure to indent descriptions longer than one line!
:description: This is a long text, which is the original description
which I have to get and store as text in my database.
# Valid tag names include tag1..tag9, and the word "quux."
:tags:
- tag1
- tag2
- tag3
# Use a full URI with scheme, and not just a domain name.
:url: http://example.com
require 'yaml'
data1 = YAML.load string_or_here_document
data2 = YAML.load_file '/path/to/yaml/file'
data1[:url]
#=> "http://example.com"
data2[:tags]
#=> ["tag1", "tag2", "tag3"]
解析YAML
您可以使用或将YAML解析为Ruby对象。例如:
# Make sure to indent descriptions longer than one line!
:description: This is a long text, which is the original description
which I have to get and store as text in my database.
# Valid tag names include tag1..tag9, and the word "quux."
:tags:
- tag1
- tag2
- tag3
# Use a full URI with scheme, and not just a domain name.
:url: http://example.com
require 'yaml'
data1 = YAML.load string_or_here_document
data2 = YAML.load_file '/path/to/yaml/file'
data1[:url]
#=> "http://example.com"
data2[:tags]
#=> ["tag1", "tag2", "tag3"]
YAML输入解析为Ruby哈希
鉴于上述YAML,在正确解析输入字符串或文件后,您将获得以下哈希对象:
{:description=>
"This is a long text, which is the original description which I have to get and store as text in my database.",
:tags=>["tag1", "tag2", "tag3"],
:url=>"http://example.com"}
然后,您可以像访问任何其他哈希对象一样访问这些值。例如:
# Make sure to indent descriptions longer than one line!
:description: This is a long text, which is the original description
which I have to get and store as text in my database.
# Valid tag names include tag1..tag9, and the word "quux."
:tags:
- tag1
- tag2
- tag3
# Use a full URI with scheme, and not just a domain name.
:url: http://example.com
require 'yaml'
data1 = YAML.load string_or_here_document
data2 = YAML.load_file '/path/to/yaml/file'
data1[:url]
#=> "http://example.com"
data2[:tags]
#=> ["tag1", "tag2", "tag3"]
我不知道你为什么这么乱。将结构化数据保持为文本的标准方法是将其转换为YAML格式。@sawa确实如此,但我希望为编写数据的人员提供最少的结构(他们不是很专业),不,我无法构造表单,因为他们只是从另一个我不控制的站点在一个文本框中输入表单。发布实际的输入和输出格式,尤其是因为你说你不控制输入格式--此外,您无法可靠地解析非结构化文本;您计划如何处理非一致性输入?