# Syntax question for gsub

**URL:** <https://rubytalk.org/t/syntax-question-for-gsub/12510>\
**Category:** ruby-talk\
**Created:** [11 July 2004 17:17 UTC](https://rubytalk.org/t/syntax-question-for-gsub/12510 "2004-07-11T17:17:26Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dirk\_Einecke](https://avatars.discourse-cdn.com/v4/letter/d/8baadc/32.png) [@Dirk\_Einecke](https://rubytalk.org/u/Dirk_Einecke)\
**Post date:** [11 July 2004 17:17 UTC](https://rubytalk.org/t/syntax-question-for-gsub/12510/1 "2004-07-11T17:17:26Z")

</div>

Hi.

I've a litte syntax question. I read a HTML file and change all between the html tag:

&nbsp;&nbsp;&nbsp;fileContent.gsub!( /\<html\>(.\*?)\<\/html\>/ ) { '\<html\>foo\</html\>' }

Now I want to set the tag type dynamicly like this:

tagName = 'html'  
fileContent.gsub!( /\<html\>(.\*?)\<\/html\>/ ) { '\<'+tagName+'\>foo\</'+tagName+'\>' }

But how can I insert the var 'tagName' in the first part  
&nbsp;&nbsp;&nbsp;( /\<html\>(.\*?)\<\/html\>/ )  
?

Can anybody help me with this small problem?

greetings  
Dirk Einecke

---

<div class="post-metadata">

**Author:** ![Marcin\_Mielzynski](https://avatars.discourse-cdn.com/v4/letter/m/a587f6/32.png) [@Marcin\_Mielzynski](https://rubytalk.org/u/Marcin_Mielzynski)\
**Post date:** [11 July 2004 17:27 UTC](https://rubytalk.org/t/syntax-question-for-gsub/12510/2 "2004-07-11T17:27:26Z")

</div>

Dnia 2004-07-11 19:14, U¿ytkownik Dirk Einecke napisa³:

> But how can I insert the var 'tagName' in the first part  
> &nbsp;&nbsp;( /\<html\>(.\*?)\<\/html\>/ )  
> ?
> 
> Can anybody help me with this small problem?
> 
> greetings  
> Dirk Einecke

try:

gsub!( /\<(.+)\>(.\*?)\<\/\1\>/ ) { "\<#{$1}\>foo\</#{$1}\>" }

or even:

gsub!( /\<(.+)\>(.\*?)\<\/\1\>/,'\<\1\>foo\<\1\>' )

Marcin Mielzynski

---

<div class="post-metadata">

**Author:** ![Laurent\_Julliard4](https://avatars.discourse-cdn.com/v4/letter/l/ebca7d/32.png) [@Laurent\_Julliard4](https://rubytalk.org/u/Laurent_Julliard4)\
**Post date:** [11 July 2004 17:27 UTC](https://rubytalk.org/t/syntax-question-for-gsub/12510/3 "2004-07-11T17:27:26Z")

</div>

maybe like this?

> > tag2='miao'

=\> "miao"

> > '\<html\>miao\</html\>'.gsub(/\<#{tag}\>.\*?\<\/#{tag}\>/, '\<'+tag2+'\>foo\</'+tag2+'\>')

=\> "\<miao\>foo\</miao\>"

otherwise you could use Regexp.compile:

> > Regexp.compile tag+'.\*?'+tag

=\> /html.\*?html/

or Regexp.new

> > Regexp.new tag+'.\*?'+tag

=\> /html.\*?html/

but, would'nt it be easier to do:

gsub /\<\/?#{tag}\>/, tag2

?

> **···**
>
> il Sun, 11 Jul 2004 19:14:19 +0200, Dirk Einecke \<dirk.einecke@gmx.de\> ha scritto::

---

<div class="post-metadata">

**Author:** ![Kristof\_Bastiaensen1](https://avatars.discourse-cdn.com/v4/letter/k/9fc29f/32.png) [@Kristof\_Bastiaensen1](https://rubytalk.org/u/Kristof_Bastiaensen1)\
**Post date:** [11 July 2004 20:37 UTC](https://rubytalk.org/t/syntax-question-for-gsub/12510/4 "2004-07-11T20:37:35Z")

</div>

What about ( /\<#{tagName}\>(.\*?)\<\/#{tagName}/ )?  
Does that solve your problem?

Kristof

> **···**
>
> On Sun, 11 Jul 2004 19:14:19 +0200, Dirk Einecke wrote:
> 
> > \<snip\>  
> > tagName = 'html'  
> > fileContent.gsub!( /\<html\>(.\*?)\<\/html\>/ ) {  
> > '\<'+tagName+'\>foo\</'+tagName+'\>' }
> > 
> > But how can I insert the var 'tagName' in the first part  
> > &nbsp;&nbsp;&nbsp;( /\<html\>(.\*?)\<\/html\>/ )  
> > ?
> > 
> > Can anybody help me with this small problem?

---

<div class="post-metadata">

**Author:** ![Randy\_Lawrence](https://avatars.discourse-cdn.com/v4/letter/r/5f9b8f/32.png) [@Randy\_Lawrence](https://rubytalk.org/u/Randy_Lawrence)\
**Post date:** [11 July 2004 23:23 UTC](https://rubytalk.org/t/syntax-question-for-gsub/12510/5 "2004-07-11T23:23:45Z")

</div>

Dirk Einecke wrote:

> Hi.
> 
> I've a litte syntax question. I read a HTML file and change all between the html tag:
> 
> &nbsp;&nbsp;fileContent.gsub!( /\<html\>(.\*?)\<\/html\>/ ) { '\<html\>foo\</html\>' }
> 
> Now I want to set the tag type dynamicly like this:
> 
> tagName = 'html'  
> fileContent.gsub!( /\<html\>(.\*?)\<\/html\>/ ) { '\<'+tagName+'\>foo\</'+tagName+'\>' }
> 
> But how can I insert the var 'tagName' in the first part  
> &nbsp;&nbsp;( /\<html\>(.\*?)\<\/html\>/ )  
> ?
> 
> Can anybody help me with this small problem?
> 
> greetings  
> Dirk Einecke

I just came across a few regex resources on the web today so I'll post:

Example Regex Expressions (Markup Category)

> **[Regular Expression Library](https://www.regexlib.com/DisplayPatterns.aspx?cattabindex=4&categoryId=8&AspxAutoDetectCookieSupport=1)**
>
> Regular Expression Library provides a searchable database of regular expressions. Users can add, edit, rate, and test regular expressions.

Regex Cheatsheet (useful despite being .NET)  
[http://www.regexlib.com/CheatSheet.htm](http://www.regexlib.com/CheatSheet.htm)

Regex Coach (free/donationware) GUI tool for both Linux and Windows  
[http://weitz.de/regex-coach/](http://weitz.de/regex-coach/)

---

<div class="post-metadata">

**Author:** ![Dirk\_Einecke](https://avatars.discourse-cdn.com/v4/letter/d/8baadc/32.png) [@Dirk\_Einecke](https://rubytalk.org/u/Dirk_Einecke)\
**Post date:** [11 July 2004 17:32 UTC](https://rubytalk.org/t/syntax-question-for-gsub/12510/6 "2004-07-11T17:32:26Z")

</div>

Hi.

Marcin Miel¿yñski wrote:

> Dnia 2004-07-11 19:14, U¿ytkownik Dirk Einecke napisa³:
> 
> > But how can I insert the var 'tagName' in the first part  
> > &nbsp;&nbsp;( /\<html\>(.\*?)\<\/html\>/ )  
> > ?
> > 
> > Can anybody help me with this small problem?
> 
> try:
> 
> gsub!( /\<(.+)\>(.\*?)\<\/\1\>/ ) { "\<#{$1}\>foo\</#{$1}\>" }
> 
> or even:
> 
> gsub!( /\<(.+)\>(.\*?)\<\/\1\>/,'\<\1\>foo\<\1\>' )

But this is only working for the tag "html". If I wont to use an other tag (like "title" or "head") I can not use your solution. ☹

greetings  
Dirk Einecke

---

<div class="post-metadata">

**Author:** ![Dirk\_Einecke](https://avatars.discourse-cdn.com/v4/letter/d/8baadc/32.png) [@Dirk\_Einecke](https://rubytalk.org/u/Dirk_Einecke)\
**Post date:** [11 July 2004 17:37 UTC](https://rubytalk.org/t/syntax-question-for-gsub/12510/7 "2004-07-11T17:37:34Z")

</div>

Hi.

gabriele renzi wrote:

> > > '\<html\>miao\</html\>'.gsub(/\<#{tag}\>.\*?\<\/#{tag}\>/, '\<'+tag2+'\>foo\</'+tag2+'\>')

That's it. Thank you very much.

greetings  
Dirk Einecke

---

<div class="post-metadata">

**Author:** ![Marcin\_Mielzynski](https://avatars.discourse-cdn.com/v4/letter/m/a587f6/32.png) [@Marcin\_Mielzynski](https://rubytalk.org/u/Marcin_Mielzynski)\
**Post date:** [11 July 2004 17:37 UTC](https://rubytalk.org/t/syntax-question-for-gsub/12510/8 "2004-07-11T17:37:26Z")

</div>

Dnia 2004-07-11 19:30, U¿ytkownik Dirk Einecke napisa³:

> But this is only working for the tag "html". If I wont to use an other tag (like "title" or "head") I can not use your solution. ☹
> 
> greetings  
> Dirk Einecke

It should work for any tag

Marcin Miel¿yñski

---

<div class="post-metadata">

**Author:** ![Dirk\_Einecke](https://avatars.discourse-cdn.com/v4/letter/d/8baadc/32.png) [@Dirk\_Einecke](https://rubytalk.org/u/Dirk_Einecke)\
**Post date:** [11 July 2004 17:42 UTC](https://rubytalk.org/t/syntax-question-for-gsub/12510/9 "2004-07-11T17:42:25Z")

</div>

Hi.

Marcin Miel¿yñski wrote:

> It should work for any tag

But I do not unterstand this:

gsub!( /\<(.+)\>(.\*?)\<\/\1\>/ ) { "\<#{$1}\>foo\</#{$1}\>" }

How this code take the var "tagName" ?

greetings  
Dirk Einecke

---

<div class="post-metadata">

**Author:** ![Marcin\_Mielzynski](https://avatars.discourse-cdn.com/v4/letter/m/a587f6/32.png) [@Marcin\_Mielzynski](https://rubytalk.org/u/Marcin_Mielzynski)\
**Post date:** [11 July 2004 17:47 UTC](https://rubytalk.org/t/syntax-question-for-gsub/12510/10 "2004-07-11T17:47:25Z")

</div>

Dnia 2004-07-11 19:40, U¿ytkownik Dirk Einecke napisa³:

> How this code take the var "tagName" ?
> 
> greetings  
> Dirk Einecke

Sorry 😃

I misunderstood Your intention.

Marcin Miel¿yñski

---

<div class="post-metadata">

**Author:** ![MATTHEW\_REUBEN\_MARGO](https://avatars.discourse-cdn.com/v4/letter/m/bc79bd/32.png) [@MATTHEW\_REUBEN\_MARGO](https://rubytalk.org/u/MATTHEW_REUBEN_MARGO)\
**Post date:** [11 July 2004 19:14 UTC](https://rubytalk.org/t/syntax-question-for-gsub/12510/11 "2004-07-11T19:14:50Z")

</div>

Dirk Einecke wrote:

> Hi.
> 
> Marcin Miel¿yñski wrote:
> 
> > It should work for any tag
> 
> But I do not unterstand this:
> 
> gsub!( /\<(.+)\>(.\*?)\<\/\1\>/ ) { "\<#{$1}\>foo\</#{$1}\>" }
> 
> How this code take the var "tagName" ?
> 
> greetings  
> Dirk Einecke

having #{anyvariable} inside of a double quoted string will put the value of anyvariable into the string. So you can do something along the lines of

tagName = "BODY"  
and then anywhere in your regular expression where you want to use the value of tagName you put "#{tagName}"

This is discussed in the pickaxe book (Programming Ruby) which is available both in book form through amazon and online at [http://www.rubycentral.com/book/\](http://www.rubycentral.com/book/%5C). If you look at the Ruby.new section in Programming Ruby you will find more description of the "#{}" syntax.

-Matthew Margolis

---

<div class="post-metadata">

**Author:** ![Chris\_Dutton1](https://avatars.discourse-cdn.com/v4/letter/c/a698b9/32.png) [@Chris\_Dutton1](https://rubytalk.org/u/Chris_Dutton1)\
**Post date:** [12 July 2004 00:12 UTC](https://rubytalk.org/t/syntax-question-for-gsub/12510/12 "2004-07-12T00:12:25Z")

</div>

Dirk Einecke wrote:

> Hi.
> 
> Marcin Miel¿yñski wrote:
> 
> > It should work for any tag
> 
> But I do not unterstand this:
> 
> gsub!( /\<(.+)\>(.\*?)\<\/\1\>/ ) { "\<#{$1}\>foo\</#{$1}\>" }
> 
> How this code take the var "tagName" ?

Should just be something like:

gsub!( /\<(#{tagName})\>(.\*?)\<\/\1\>/ ) { "\<#{$1}\>foo\</#{$1}\>" }
