Syntax question for gsub

Discussion in 'Ruby' started by Dirk Einecke, Jul 11, 2004.

  1. Dirk Einecke

    Dirk Einecke Guest

    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
    Dirk Einecke, Jul 11, 2004
    #1
    1. Advertising

  2. 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
    Marcin Miel¿yñski, Jul 11, 2004
    #2
    1. Advertising

  3. il Sun, 11 Jul 2004 19:14:19 +0200, Dirk Einecke <>
    ha scritto::

    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

    ?
    gabriele renzi, Jul 11, 2004
    #3
  4. Dirk Einecke

    Dirk Einecke Guest

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

    greetings
    Dirk Einecke
    Dirk Einecke, Jul 11, 2004
    #4
  5. 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
    Marcin Miel¿yñski, Jul 11, 2004
    #5
  6. Dirk Einecke

    Dirk Einecke Guest

    Hi.

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


    That's it. Thank you very much.

    greetings
    Dirk Einecke
    Dirk Einecke, Jul 11, 2004
    #6
  7. Dirk Einecke

    Dirk Einecke Guest

    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
    Dirk Einecke, Jul 11, 2004
    #7
  8. 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
    Marcin Miel¿yñski, Jul 11, 2004
    #8
  9. 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
    Matthew Margolis, Jul 11, 2004
    #9
  10. 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?
    >


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

    Kristof
    Kristof Bastiaensen, Jul 11, 2004
    #10
  11. 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)
    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/
    Randy Lawrence, Jul 12, 2004
    #11
  12. Dirk Einecke

    Chris Dutton Guest

    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}>" }
    Chris Dutton, Jul 12, 2004
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Daniel Bretoi

    gsub question

    Daniel Bretoi, Dec 17, 2003, in forum: Ruby
    Replies:
    2
    Views:
    105
    Jamis Buck
    Dec 17, 2003
  2. mss
    Replies:
    5
    Views:
    109
    James Edward Gray II
    Jan 20, 2005
  3. aurelianito

    gsub and gsub! are inconsistent

    aurelianito, Nov 8, 2005, in forum: Ruby
    Replies:
    9
    Views:
    160
    Robert Klemme
    Nov 9, 2005
  4. Hoppy

    gsub method question

    Hoppy, May 10, 2006, in forum: Ruby
    Replies:
    4
    Views:
    84
    Hoppy
    May 10, 2006
  5. x1
    Replies:
    4
    Views:
    135
Loading...

Share This Page