Rails regex: Groups inside groups
I am trying to to extract a name from two possible strings.
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")
I would like to a开发者_如何学Cccess the [a-zA-Z0-9_]
with the the same group.
You can do it in .NET like this:
(\w*)(?:(?= has recruited too many people today\.)|(?<=You are being recruited into the army of \1))
But I don't think this will work in Ruby since it requires indefinite repetition inside lookbehind. Perhaps you could give it a try?
精彩评论