.* won’t match over multiple lines without the m modifier on the
RegExp, which I think is the OP’s problem:
That can’t be the OP’s problem since the OP doesn’t actually use .* in
his
regexp (or any other kind of wildcard). He was asking how to use
wildcards
so he could simplify his regexp (and make it work).
To know why his original regexp didn’t work, we’d have to see the string
it’s
supposed to match, I suppose.
Thanks for taking the time to reply. The string.gsub(/start.*end/m,
‘some_value’) did indeed help, but I am afraid my problem is a bit more
complicated.
I am basically trying to cleanup a long xml file. A typical part of the
string looks like this:
I am trying to format it like this:
Public
Tom Jones
So the problem is that the values in the xml change throughout the
string, so I cannot do a pattern match for them directly. Any ideas
would be hugely appreciated!
Posted via http://www.ruby-forum.com/.
Without knowing the whole problem it is difficult to say what the
best solution is, but for the string you post above, I would clean it
up and parse with something like Hpricot:
Thanks, I will have a try.
By the way, I see you are in Kyoto. I am studying at Tsukuba University
(about an hour from Tokyo), so if you come to the big city, I owe you a
beer!
Posted via http://www.ruby-forum.com/.
Without knowing the whole problem it is difficult to say what the
best solution is, but for the string you post above, I would clean it
up and parse with something like Hpricot:
require ‘rubygems’
require ‘hpricot’
string = DATA.read #read in string
string.gsub!(/</,‘<’) #Convert lt and gt symbols to real <>
string.gsub!(/>/,‘>’)
string.gsub!(/"/,‘"’) #Put in quotes
doc = Hpricot(string) #Parse with Hpricot
fields = [‘audience’,‘creator’] #Create array of ‘fields’ to extract
fields.each do |f| #For each field…
el = doc.search(“//div[@class=‘field field-type-text field-field-#
{f}’]”) #…find appropriate divs
el.each do |e| # for each field div…
puts “<#{f}>” + e.at(“//div[@class=‘field-item’]”).inner_html +
“</#{f}>” #print data
end
end
END
<div class="field field-type-text field-field-audience">