regexp to pars a HTML tag value

M

Mehdi Karamnejad

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
 
M

Marnen Laibow-Koser

Mehdi said:
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
(e-mail address removed)
 
M

Mike Dalessio

[Note: parts of this message were removed to make it a legal post.]


With Nokogiri, this would be:

fragment = Nokogiri::HTML.fragment(MyStr)
fragment.at_css("span#MySpan.current").content = 'another value'
puts fragment.to_html
 
M

Mehdi Karamnejad

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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,992
Messages
2,570,220
Members
46,805
Latest member
ClydeHeld1

Latest Threads

Top