首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Ruby正则表达式中访问同一匹配组的不同匹配项?

如何在Ruby正则表达式中访问同一匹配组的不同匹配项?
EN

Stack Overflow用户
提问于 2012-08-22 02:51:42
回答 3查看 238关注 0票数 2

我有一个有多个匹配项的正则表达式。我算出了$1,$2等等。可用于访问匹配组。但是如何访问同一匹配组的多个匹配项?

请看下面的小红页。

http://rubular.com/r/nqHP1qAqRY

所以现在$1等于916,$2等于零。我怎样才能进入229885?有没有类似于11美元左右的东西?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-08-22 05:02:07

为了扩展我的评论并回答你的问题:

如果要将值存储在数组中,请修改块并收集,而不是迭代:

代码语言:javascript
复制
> arr = xml.grep(/<DATA size="(\d+)"/).collect { |d| d.match /\d+/ }
> arr.each { |a| puts "==> #{a}" }
==> 916
==> 229885

|d|是普通的Ruby块参数语法;每个d都是匹配的字符串,从中提取数字。它不是最干净的Ruby,尽管它是功能强大的。

我仍然推荐使用解析器;请注意,rexml版本应该是这样(或多或少):

代码语言:javascript
复制
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,您可以获得更有用的数据:

代码语言:javascript
复制
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
票数 1
EN

Stack Overflow用户

发布于 2012-08-22 03:11:16

首先,仅使用正则表达式解析基于xml的数据并不是一个好主意。相反,使用一个库来解析xml文件,比如nokogiri。

但是,如果您确定要使用此方法,则需要了解以下内容。正则表达式引擎在得到(令人满意的)匹配后立即停止。因此,您不能期望从一个regex-call中获得一个字符串中的所有可能的匹配,您需要在每个已经出现的匹配之后,应用一个新的regex-match遍历该字符串。你可以这样做:

代码语言:javascript
复制
# 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中工作(方法定义在这些版本中不同)。

代码语言:javascript
复制
# 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
票数 3
EN

Stack Overflow用户

发布于 2012-08-22 02:58:58

这在regex的大多数实现中是不可能的。(只有.NET可以执行此操作。)

您将不得不使用另一种解决方案,例如使用scan()Equivalent to Python’s findall() method in Ruby?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12061200

复制
相关文章

相似问题

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