Syntax question for gsub

D

Dirk Einecke

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
 
M

Marcin Miel¿yñski

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
 
G

gabriele renzi

il Sun, 11 Jul 2004 19:14:19 +0200, Dirk Einecke <[email protected]>
ha scritto::

maybe like this?=> "<miao>foo</miao>"

otherwise you could use Regexp.compile:
=> /html.*?html/

or Regexp.new=> /html.*?html/

but, would'nt it be easier to do:

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

?
 
D

Dirk Einecke

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


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
 
M

Marcin Miel¿yñski

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
 
D

Dirk Einecke

Hi.
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
 
M

Marcin Miel¿yñski

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

How this code take the var "tagName" ?

greetings
Dirk Einecke


Sorry :D

I misunderstood Your intention.

Marcin Miel¿yñski
 
M

Matthew Margolis

Dirk said:
Hi.



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
 
K

Kristof Bastiaensen

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

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

Kristof
 
R

Randy Lawrence

Dirk said:
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)
http://www.regexlib.com/DisplayPatterns.aspx?cattabindex=4&categoryId=8

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

Chris Dutton

Dirk said:
Hi.



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

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top