Syntax question for gsub

Hi.

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

   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
   ( /<html>(.*?)<\/html>/ )
?

Can anybody help me with this small problem?

greetings
Dirk Einecke

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

But how can I insert the var 'tagName' in the first part
  ( /<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

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::

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
   ( /<html>(.*?)<\/html>/ )
?

Can anybody help me with this small problem?

Dirk Einecke wrote:

Hi.

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

  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
  ( /<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)

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

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

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
  ( /<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. :frowning:

greetings
Dirk Einecke

Hi.

gabriele renzi wrote:

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

That's it. Thank you very much.

greetings
Dirk Einecke

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. :frowning:

greetings
Dirk Einecke

It should work for any tag

Marcin Miel¿yñski

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

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

How this code take the var "tagName" ?

greetings
Dirk Einecke

Sorry :smiley:

I misunderstood Your intention.

Marcin Miel¿yñski

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/\. If you look at the Ruby.new section in Programming Ruby you will find more description of the "#{}" syntax.

-Matthew Margolis

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}>" }