Rails正则表达式:组内的组

时间:2011-05-07 15:35:07

标签: ruby-on-rails regex

我试图从两个可能的字符串中提取名称。

require 'rubygems'
require 'nokogiri'
require 'open-uri'

doc = Nokogiri::HTML(open('http://www.darkthrone.com/recruiter/outside/B7OE4OA0OD8OD5OF1'))

reg = /([a-zA-Z0-9_]*) has recruited too many people today.|You are being recruited into the army of ([a-zA-Z0-9_]*)/

puts doc.text.match(reg).to_s.gsub(reg,"\\1")

doc = Nokogiri::HTML(open('http://www.darkthrone.com/recruiter/outside/B7OD8OE6OC2OF9OD5'))

puts doc.text.match(reg).to_s.gsub(reg,"\\2")

我想访问同一组的[a-zA-Z0-9_]

1 个答案:

答案 0 :(得分:0)

你可以在.NET中这样做:

(\w*)(?:(?= has recruited too many people today\.)|(?<=You are being recruited into the army of \1))

但我不认为这会在Ruby中起作用,因为它需要在lookbehind中无限重复。也许你可以尝试一下?