2011-05-07 35 views
0

我想從兩個可能的字符串中提取一個名稱。Rails的正則表達式:組內組

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_]

+0

這是很不清楚你的問題。嘗試提供一些預期的輸入/輸出,或者提供您想要實現的功能的僞代碼。 – 2011-05-07 15:45:10

+0

我想我知道你在做什麼,我不知道是否有可能,除非Ruby支持命名組。可能還沒有,因爲你需要給這兩個團體同名。你確定他們需要在同一組? – 2011-05-07 16:10:58

+0

謝謝,但我用'scan()'解決了它 – 2011-05-07 17:02:57

回答

0

你可以做到這一點在.NET這樣的:

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

但我不認爲這會在Ruby中工作,因爲它需要內部的回顧後無限期重複。也許你可以試試看?