Regexp to pars a HTML tag value

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</span>
above VALUE is what I want to replace with another value

···

--
Posted via http://www.ruby-forum.com/.

Mehdi Karamnejad 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</span>
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
marnen@marnen.org
--
Posted via http://www.ruby-forum.com/\.

Mehdi Karamnejad 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</span>
> 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

···

On Tue, Dec 15, 2009 at 11:10 AM, Marnen Laibow-Koser <marnen@marnen.org>wrote:

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
marnen@marnen.org
--
Posted via http://www.ruby-forum.com/\.

--
mike dalessio
mike@csa.net

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?

···

--
Posted via http://www.ruby-forum.com/.

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 Karamnejad <sepehr.online@gmail.com>wrote:

--
Posted via http://www.ruby-forum.com/\.

--
mike dalessio
mike@csa.net

This stackoverflow post might help:

  html - RegEx match open tags except XHTML self-contained tags - Stack Overflow

···

On Wed, Dec 16, 2009 at 03:53:35AM +0900, Mehdi Karamnejad 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?

--
Aaron Patterson
http://tenderlovemaking.com/