我有一个有多个匹配项的正则表达式。我算出了$1,$2等等。可用于访问匹配组。但是如何访问同一匹配组的多个匹配项?
请看下面的小红页。
http://rubular.com/r/nqHP1qAqRY
所以现在$1等于916,$2等于零。我怎样才能进入229885?有没有类似于11美元左右的东西?
发布于 2012-08-22 05:02:07
为了扩展我的评论并回答你的问题:
如果要将值存储在数组中,请修改块并收集,而不是迭代:
> arr = xml.grep(/<DATA size="(\d+)"/).collect { |d| d.match /\d+/ }
> arr.each { |a| puts "==> #{a}" }
==> 916
==> 229885|d|是普通的Ruby块参数语法;每个d都是匹配的字符串,从中提取数字。它不是最干净的Ruby,尽管它是功能强大的。
我仍然推荐使用解析器;请注意,rexml版本应该是这样(或多或少):
require 'rexml/document'
include REXML
doc = Document.new xml
arr = doc.elements.collect("//DATA") { |d| d.attributes["size"] }
arr.each { |a| puts "==> #{a}" }一旦您的" XML“被转换为实际的XML,您可以获得更有用的数据:
doc = Document.new xml
arr = doc.elements.collect("//file") do |f|
name = f.elements["FILENAME"].attributes["path"]
size = f.elements["DATA"].attributes["size"]
[name, size]
end
arr.each { |a| puts "#{a[0]}\t#{a[1]}" }
~/Users/1.txt 916
~/Users/2.txt 229885发布于 2012-08-22 03:11:16
首先,仅使用正则表达式解析基于xml的数据并不是一个好主意。相反,使用一个库来解析xml文件,比如nokogiri。
但是,如果您确定要使用此方法,则需要了解以下内容。正则表达式引擎在得到(令人满意的)匹配后立即停止。因此,您不能期望从一个regex-call中获得一个字符串中的所有可能的匹配,您需要在每个已经出现的匹配之后,应用一个新的regex-match遍历该字符串。你可以这样做:
# ruby 1.9.x version
regex = /<DATA size="(\d+)"/
str = your_string # Your string to be parsed
position = 0
matches = []
while(match = regex.match(str,position)) do # Until there are no matches anymore
position = match.end 0 # set position to the end of the last match
matches << match[1] # add the matched number to the matches-array
end在此之后,所有已解析的数字都应该在matches中。
但是由于您的评论表明您正在使用ruby 1.8.x,我将在此处发布另一个版本,该版本在1.8.x中工作(方法定义在这些版本中不同)。
# ruby 1.8.x version
regex = /<DATA size="(\d+)"/
str = your_string # Your string to be parsed
matches = []
while(match = regex.match(str)) do # Until there are no matches anymore
str = match.post_match # set str to the part which is after the match.
matches << match[1] # add the matched number to the matches-array
end发布于 2012-08-22 02:58:58
这在regex的大多数实现中是不可能的。(只有.NET可以执行此操作。)
您将不得不使用另一种解决方案,例如使用scan():Equivalent to Python’s findall() method in Ruby?。
https://stackoverflow.com/questions/12061200
复制相似问题