# Regexp to pars a HTML tag value

**URL:** <https://www.ruby-forum.com/t/regexp-to-pars-a-html-tag-value/180239>\
**Category:** Ruby\
**Created:** [December 15, 2009, 4:38pm UTC](https://www.ruby-forum.com/t/regexp-to-pars-a-html-tag-value/180239 "2009-12-15T16:38:09Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![sepehr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@sepehr](https://www.ruby-forum.com/u/sepehr)\
**Post date:** [December 15, 2009, 4:38pm UTC](https://www.ruby-forum.com/t/regexp-to-pars-a-html-tag-value/180239/1 "2009-12-15T16:38:09Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![sepehr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@sepehr](https://www.ruby-forum.com/u/sepehr)\
**Post date:** [December 15, 2009, 5:10pm UTC](https://www.ruby-forum.com/t/regexp-to-pars-a-html-tag-value/180239/2 "2009-12-15T17:10:21Z")

</div>

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](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![sepehr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@sepehr](https://www.ruby-forum.com/u/sepehr)\
**Post date:** [December 15, 2009, 6:08pm UTC](https://www.ruby-forum.com/t/regexp-to-pars-a-html-tag-value/180239/3 "2009-12-15T18:08:33Z")

</div>

On Tue, Dec 15, 2009 at 11:10 AM, Marnen Laibow-Koser  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)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.

---

<div class="post-metadata">

**Author:** ![sepehr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@sepehr](https://www.ruby-forum.com/u/sepehr)\
**Post date:** [December 15, 2009, 7:53pm UTC](https://www.ruby-forum.com/t/regexp-to-pars-a-html-tag-value/180239/4 "2009-12-15T19:53:33Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![sepehr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@sepehr](https://www.ruby-forum.com/u/sepehr)\
**Post date:** [December 15, 2009, 8:52pm UTC](https://www.ruby-forum.com/t/regexp-to-pars-a-html-tag-value/180239/5 "2009-12-15T20:52:24Z")

</div>

On Tue, Dec 15, 2009 at 1:53 PM, Mehdi K.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)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?

> <https://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454>

---

<div class="post-metadata">

**Author:** ![sepehr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@sepehr](https://www.ruby-forum.com/u/sepehr)\
**Post date:** [December 15, 2009, 8:55pm UTC](https://www.ruby-forum.com/t/regexp-to-pars-a-html-tag-value/180239/6 "2009-12-15T20:55:55Z")

</div>

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](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454)
