Ruby - Hi, I have max 200 html h3 headings in a html document. I am deleteing all, with characters between the two tags, using x.gsub(/\<h3\>(.*)\<\/h3\>/, '<h3>Chapter </h3>')
. My problem is: I need to insert the individual chapter numbers 1, 2, 3 and so on. Is this possible using a hash or some other way?
You could do this...
# Establish a counter
i = 0
x.gsub( %r{<h3>(.+?)</h3>}i ){ |match| i+=1; "Chapter #{i}" }
The %r{...}
is another way of defining a RegExp literal ( /.../
), it helps here, so you don't need to escape the slash; looks a bit cleaner...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.