I want to replace the value inside a HTML tag with another value using
gsub function and regexp,what is the best regexp for this purpose?
puts MyStr.gsub(/“What Should Be the Regex?”/, “NewValue”)
example:
<span id=“MySpan” class="current>VALUE
above VALUE is what I want to replace with another value
Mehdi K. wrote:
I want to replace the value inside a HTML tag with another value using
gsub function and regexp,what is the best regexp for this purpose?
puts MyStr.gsub(/“What Should Be the Regex?”/, “NewValue”)
example:
<span id=“MySpan” class="current>VALUE
above VALUE is what I want to replace with another value
If you expect any complexity at all in the structure of the HTML, then
regexes will be inadequate and you’ll need to use a parser such as
Nokogiri.
Best,
–Â
Marnen Laibow-Koser
http://www.marnen.org
[email protected]
On Tue, Dec 15, 2009 at 11:10 AM, Marnen Laibow-Koser
[email protected]wrote:
Mehdi K. wrote:
I want to replace the value inside a HTML tag with another value using
gsub function and regexp,what is the best regexp for this purpose?
puts MyStr.gsub(/“What Should Be the Regex?”/, “NewValue”)
example:
<span id=“MySpan” class="current>VALUE
above VALUE is what I want to replace with another value
With Nokogiri, this would be:
fragment = Nokogiri::HTML.fragment(MyStr)
fragment.at_css(“span#MySpan.current”).content = ‘another value’
puts fragment.to_html
If you expect any complexity at all in the structure of the HTML, then
regexes will be inadequate and you’ll need to use a parser such as
Nokogiri.
I’ve used Nokogiri before, I don’t want to use it in this scenario
because in the string there is only one tag(which is span) and it’s
simple,any ideas?
On Tue, Dec 15, 2009 at 1:53 PM, Mehdi K.
[email protected]wrote:
I’ve used Nokogiri before, I don’t want to use it in this scenario
because in the string there is only one tag(which is span) and it’s
simple,any ideas?
On Wed, Dec 16, 2009 at 03:53:35AM +0900, Mehdi K. wrote:
I’ve used Nokogiri before, I don’t want to use it in this scenario
because in the string there is only one tag(which is span) and it’s
simple,any ideas?
This stackoverflow post might help:
html - RegEx match open tags except XHTML self-contained tags - Stack Overflow