The browser page I'm working with has a semi-variable text group: "Files
(12)"
I want to turn that into a variable in my Ruby script: myfiles = "Files
(12)"
I surprised myself and figured out the regex to account for the changing
count: Files \(\d{0,999}\) but how do I grab it off of the browser
page?
So, you want to fetch the page calling the URL, locate that text and
extract the number part between parens?
If so, I would use open-uri to read the page, then use Nokogiri to
parse it, come up with an XPath or css selector expression that takes
you to the text node, get the contents of the node, and then apply
your regular expression to that. Something like:
require 'open-uri'
require 'nokogiri' # 'gem install nokogiri' first
doc = Nokogiri::HTML(open(your_url).read)
element = doc.css("your css selector expression here")
# or element = doc.xpath("your xpath expression here") if you find it
more suitable
m = element.text.match /Files \((\d+)\)/
puts m[1] if m
By the way, \d{0,999} doesn't mean a number between 0 and 999, it
means a digit repeated between 0 and 999 times, so for example this
would match "Files ()", as well as "Files (23423283423423423423423)".
You might to make it a little bit more flexible with the whitespace
between the word Files and the parens, something like
/Files\s+\((\d+)\)/
Hope this helps,
Jesus.