[yanq = yet another newbie question] correct syntax fovariable and regexp

Discussion in 'Ruby' started by Yvon Thoraval, Oct 5, 2004.

  1. i have a regex like :

    rex = Regexp.new("^(.*)\s+Bourgogne[\\s*|\\s+(.*)]")

    but i want to change the string "Bourgogne" to the third arg of the
    script (needle = ARGV[2].to_s)

    what is the correct syntax in order to replace "Bourgogne" by that
    variable ?

    Also, i do have a test :

    if line =~ /^(.*)\s+Bourgogne\s*(.*)/

    using rex is the correct syntax :

    if line =~ rex

    ???


    --
    yt
    Yvon Thoraval, Oct 5, 2004
    #1
    1. Advertising

  2. Yvon Thoraval

    Markus Guest

    Re: [yanq = yet another newbie question] correct syntax fovariableand regexp

    I am not sure exactly what you are asking, so I will offer multiple
    suggestions and you can see what works best for you:

    line.gsub!(/Bourgogne/,needle)

    changes all occurrences

    line.sub!(/Bourgogne/,needle)

    changes the first occurrence (if any), as does

    line[/Bourgogne/] = needle

    or if you want to keep the original string you can write (note, no "!"):

    new_line = line.sub(/Bourgogne/,needle)

    or maybe you would rather:

    rex = Regexp.new("^(.*)Bourgogne(.*)")
    if line =~ rex
    line = $1 + needle + $2
    end

    Hope this helps.

    -- Markus


    On Mon, 2004-10-04 at 23:39, Yvon Thoraval wrote:
    > i have a regex like :
    >
    > rex = Regexp.new("^(.*)\s+Bourgogne[\\s*|\\s+(.*)]")
    >
    > but i want to change the string "Bourgogne" to the third arg of the
    > script (needle = ARGV[2].to_s)
    >
    > what is the correct syntax in order to replace "Bourgogne" by that
    > variable ?
    >
    > Also, i do have a test :
    >
    > if line =~ /^(.*)\s+Bourgogne\s*(.*)/
    >
    > using rex is the correct syntax :
    >
    > if line =~ rex
    >
    > ???
    >
    Markus, Oct 5, 2004
    #2
    1. Advertising

  3. Re: [yanq = yet another newbie question] correct syntax fovariable and regexp

    Markus <> wrote:

    > I am not sure exactly what you are asking, so I will offer multiple
    > suggestions and you can see what works best for you:

    Thanks for your answer, i need to clarify...

    I want, from a given file, output to a new file the lines containing a
    word (the example was "Bourgogne") given as the third arg of the script.

    Notice i'm able to do that in a "static" way that's to say changing the
    word in the script's regexp, what i don't know is the correct syntax to
    replace this word by a variable in the regexp, the variable being a
    String (ARGV[2].to_s).
    --
    yt
    Yvon Thoraval, Oct 5, 2004
    #3
  4. Re: [yanq = yet another newbie question] correct syntax fovariable and regexp

    "Yvon Thoraval" <> schrieb im Newsbeitrag
    news:1gl6f5h.1hzh8w1ct4lg5N%...
    > Markus <> wrote:
    >
    > > I am not sure exactly what you are asking, so I will offer multiple
    > > suggestions and you can see what works best for you:

    > Thanks for your answer, i need to clarify...
    >
    > I want, from a given file, output to a new file the lines containing a
    > word (the example was "Bourgogne") given as the third arg of the script.
    >
    > Notice i'm able to do that in a "static" way that's to say changing the
    > word in the script's regexp, what i don't know is the correct syntax to
    > replace this word by a variable in the regexp, the variable being a
    > String (ARGV[2].to_s).
    > --
    > yt


    needle = ARGV[2]
    rx = %r{^(.*)\s+#{needle}[\\s*|\\s+(.*)]}

    If you want to be sure that meta symbols in needle are not interpreted you
    can do this:

    rx = %r{^(.*)\s+#{Regexp.escape needle}[\\s*|\\s+(.*)]}

    What strikes me odd is the rest of your regexp, especially the square
    brackets. If you just want to match a certain word as word in the line
    you should probably use these ones:

    rx = %r{\b#{needle}\b}
    rx = %r{\b#{Regexp.escape needle}\b}

    "\b" matches a word boundary.

    Kind regards

    robert
    Robert Klemme, Oct 5, 2004
    #4
  5. Re: [yanq = yet another newbie question] correct syntax fovariable and regexp

    Robert Klemme <> wrote:

    > needle = ARGV[2]
    > rx = %r{^(.*)\s+#{needle}[\\s*|\\s+(.*)]}
    >
    > If you want to be sure that meta symbols in needle are not interpreted you
    > can do this:
    >
    > rx = %r{^(.*)\s+#{Regexp.escape needle}[\\s*|\\s+(.*)]}
    >
    > What strikes me odd is the rest of your regexp, especially the square
    > brackets.
    >If you just want to match a certain word as word in the line
    > you should probably use these ones:
    >
    > rx = %r{\b#{needle}\b}
    > rx = %r{\b#{Regexp.escape needle}\b}
    >
    > "\b" matches a word boundary.


    thanks for all, crystall clear to me °;)
    (i had forgotten the magic of "\b")

    and works great !!!


    --
    yt
    Yvon Thoraval, Oct 5, 2004
    #5
    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. Philip Meyer
    Replies:
    0
    Views:
    403
    Philip Meyer
    Nov 30, 2003
  2. Berehem
    Replies:
    4
    Views:
    536
    Lawrence Kirby
    Apr 28, 2005
  3. basi
    Replies:
    7
    Views:
    85
    William James
    Aug 15, 2005
  4. basi

    YANQ: Nesting in YAML

    basi, Aug 18, 2005, in forum: Ruby
    Replies:
    12
    Views:
    163
  5. Joao Silva
    Replies:
    16
    Views:
    337
    7stud --
    Aug 21, 2009
Loading...

Share This Page