# Regexp to pars a HTML tag value

**URL:** <https://rubytalk.org/t/regexp-to-pars-a-html-tag-value/56654>\
**Category:** ruby-talk\
**Created:** [15 December 2009 15:38 UTC](https://rubytalk.org/t/regexp-to-pars-a-html-tag-value/56654 "2009-12-15T15:38:25Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mehdi\_Karamnejad](https://avatars.discourse-cdn.com/v4/letter/m/ce7236/32.png) [@Mehdi\_Karamnejad](https://rubytalk.org/u/Mehdi_Karamnejad)\
**Post date:** [15 December 2009 15:38 UTC](https://rubytalk.org/t/regexp-to-pars-a-html-tag-value/56654/1 "2009-12-15T15:38:25Z")

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

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Marnen\_Laibow-Koser](https://avatars.discourse-cdn.com/v4/letter/m/a587f6/32.png) [@Marnen\_Laibow-Koser](https://rubytalk.org/u/Marnen_Laibow-Koser)\
**Post date:** [15 December 2009 16:10 UTC](https://rubytalk.org/t/regexp-to-pars-a-html-tag-value/56654/2 "2009-12-15T16:10:23Z")

</div>

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](http://www.marnen.org)  
> marnen@marnen.org  
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Mike\_Dalessio1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/mike_dalessio1/32/1903_2.png) [@Mike\_Dalessio1](https://rubytalk.org/u/Mike_Dalessio1)\
**Post date:** [15 December 2009 16:58 UTC](https://rubytalk.org/t/regexp-to-pars-a-html-tag-value/56654/3 "2009-12-15T16:58:39Z")

</div>

> 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](http://www.marnen.org)  
> > marnen@marnen.org  
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
> 
> --  
> mike dalessio  
> mike@csa.net

---

<div class="post-metadata">

**Author:** ![Mehdi\_Karamnejad](https://avatars.discourse-cdn.com/v4/letter/m/ce7236/32.png) [@Mehdi\_Karamnejad](https://rubytalk.org/u/Mehdi_Karamnejad)\
**Post date:** [15 December 2009 18:53 UTC](https://rubytalk.org/t/regexp-to-pars-a-html-tag-value/56654/4 "2009-12-15T18:53:35Z")

</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?

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Mike\_Dalessio1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/mike_dalessio1/32/1903_2.png) [@Mike\_Dalessio1](https://rubytalk.org/u/Mike_Dalessio1)\
**Post date:** [15 December 2009 19:44 UTC](https://rubytalk.org/t/regexp-to-pars-a-html-tag-value/56654/5 "2009-12-15T19:44: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?

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

> **···**
>
> On Tue, Dec 15, 2009 at 1:53 PM, Mehdi Karamnejad \<sepehr.online@gmail.com\>wrote:
> 
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
> 
> --  
> mike dalessio  
> mike@csa.net

---

<div class="post-metadata">

**Author:** ![Aaron\_Patterson1](https://avatars.discourse-cdn.com/v4/letter/a/e0b2c6/32.png) [@Aaron\_Patterson1](https://rubytalk.org/u/Aaron_Patterson1)\
**Post date:** [15 December 2009 19:45 UTC](https://rubytalk.org/t/regexp-to-pars-a-html-tag-value/56654/6 "2009-12-15T19:45:23Z")

</div>

This stackoverflow post might help:

&nbsp;&nbsp;[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)

> **···**
>
> 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/](http://tenderlovemaking.com/)
