Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从XML标记中提取信息-RUBY_Ruby_Xml - Fatal编程技术网

从XML标记中提取信息-RUBY

从XML标记中提取信息-RUBY,ruby,xml,Ruby,Xml,我有这样一个xml标记 <cvParam cvRef="MS" accession="MS:1000511" name="ms level" value="1"/> 如何获取ms级别(1)的值? 请不要推荐nokogiri或rexml。我想学习如何在逐行读取文件时解析信息。 谢谢。str='' str = '<cvParam cvRef="MS" accession="MS:1000511" name="ms level" value="1"/>' md

我有这样一个xml标记

    <cvParam cvRef="MS" accession="MS:1000511" name="ms level" value="1"/>

如何获取ms级别(1)的值? 请不要推荐nokogiri或rexml。我想学习如何在逐行读取文件时解析信息。 谢谢。

str=''
str = '<cvParam cvRef="MS" accession="MS:1000511" name="ms level" value="1"/>'

md = str.match(/
     name="(.*?)"        #Match name=" followed by any character(.), 0 or more times(*), captured in group 1 (), followed by...
     \s*                 #whitespace(\s), 0 or more times(*), followed by...
     value="(\d+)"       #value=", followed by a number(\d), one or more times(+), captured in group 2 ().
/x) #x-flag: ignore whitespace in pattern, which allows you to write the regex on mutliple lines and add comments like these.

if md
  name, value = md.captures   #captures => an Array containing the matches for each of the parenthesized groups in the regex.
  puts "#{name}(#{value})"
end

--output:--
ms level(1)
md=str.match(/ name=“(.*?”#Match name=“后跟任何字符(.),0或更多次(*),在组1()中捕获,后跟。。。 \s*#空格(\s),0或更多次(*),后跟。。。 value=“(\d+)”#value=“,后跟一个数字(\d),一次或多次(+),在组2中捕获()。 /x) #x-flag:忽略模式中的空白,这允许您在多行上编写正则表达式,并添加如下注释。 如果md name,value=md.captures#captures=>包含正则表达式中每个括号组的匹配项的数组。 放置“#{name}(#{value})” 结束 --输出:-- ms级别(1)
这个答案是给那些还没有过早排除Nokogiri的读者的。您可以逐行处理文件,并将每一行作为
DocumentFragment
处理

require 'nokogiri'

line = '<cvParam cvRef="MS" accession="MS:1000511" name="ms level" value="1"/>'

fragment = Nokogiri::HTML.fragment(line)
cvparam = fragment.first_element_child

puts cvparam.attributes.values_at('name', 'value')

#=> ["ms level", "1"]
需要“nokogiri”
行=“”
fragment=Nokogiri::HTML.fragment(行)
cvparam=fragment.first\u元素\u子元素
将cvparam.attributes.values_置于('name','value'))
#=>[“毫秒级”,“1”]
请不要建议使用nokogiri或rexml。--在评论中如何?根据实际字符串的不同,上面的正则表达式有很多种中断方式。当你厌倦了一次又一次地修补正则表达式来处理无法正确解析的新字符串,而你的正则表达式是一个混乱、混乱、不可读、无法维护的烂摊子时,你就会明白为什么人们建议使用nokogiri。另一方面,rexml是来自被称为Ruby标准库的荒原的遗物?有几种方法可以一次处理一点文件。