How do I use a variable in a regexp search string?

Discussion in 'Ruby' started by joemac, Jan 30, 2010.

  1. joemac

    joemac Guest

    Hi All,

    I dont know how to use a variable in a regex searchstring.
    ruby seems to only handle hardcoded serachstring in
    regex's. How do you do this. My code is below.

    Also - how do you put a variable name in a
    File.open("filename.txt") statement like this:
    File.open(filevariable) where filevariable can
    be set to anything? I could not find this in
    the docs.

    Thanks, --Joe
    ================ snip code ===============
    #!/usr/bin/ruby

    F = "filename.txt"
    searchstring = ARGV[0]

    print "searchstring is ", searchstring, "\n"


    File.open("filename.txt").each { |line|
    # this fails puts line if line =~ searchstring
    # this fails puts line if line =~ /searchstring/
    # this fails puts line if line =~ "searchstring"

    #this works
    # puts line
    }
     
    joemac, Jan 30, 2010
    #1
    1. Advertising

  2. >
    > I dont know how to use a variable in a regex searchstring.
    > ruby seems to only handle hardcoded serachstring in
    > regex's. =A0 How do you do this. =A0 My code is below.
    >


    Does this help?

    str =3D "abcdef"
    searchstring =3D "cd"
    p str =3D~ /#{searchstring}/


    Harry
     
    Harry Kakueki, Jan 30, 2010
    #2
    1. Advertising

  3. joemac

    Intransition Guest

    On Jan 29, 10:38=A0pm, Harry Kakueki <> wrote:
    > > I dont know how to use a variable in a regex searchstring.
    > > ruby seems to only handle hardcoded serachstring in
    > > regex's. =A0 How do you do this. =A0 My code is below.

    >
    > Does this help?
    >
    > str =3D "abcdef"
    > searchstring =3D "cd"
    > p str =3D~ /#{searchstring}/


    You may need to escape:

    p str =3D~ /#{Regexp.escape(searchstring)}/
     
    Intransition, Jan 30, 2010
    #3
  4. Thomas Sawyer wrote:
    > On Jan 29, 10:38�pm, Harry Kakueki <> wrote:
    >> > I dont know how to use a variable in a regex searchstring.
    >> > ruby seems to only handle hardcoded serachstring in
    >> > regex's. � How do you do this. � My code is below.

    >>
    >> Does this help?
    >>
    >> str = "abcdef"
    >> searchstring = "cd"
    >> p str =~ /#{searchstring}/

    >
    > You may need to escape:
    >
    > p str =~ /#{Regexp.escape(searchstring)}/


    But note that #{} only makes sense if you're interpolating a variable
    into part of a regex -- say, /January #{year}/. If the variable is the
    whole regex, then it's more sensible to do Regex.new(searchstring).


    Best,
    --
    Marnen Laibow-Koser
    http://www.marnen.org

    --
    Posted via http://www.ruby-forum.com/.
     
    Marnen Laibow-Koser, Jan 30, 2010
    #4
  5. joemac

    Josh Cheek Guest

    [Note: parts of this message were removed to make it a legal post.]

    On Fri, Jan 29, 2010 at 6:50 PM, joemac <> wrote:

    > Hi All,
    >
    > I dont know how to use a variable in a regex searchstring.
    > ruby seems to only handle hardcoded serachstring in
    > regex's. How do you do this. My code is below.
    >
    > Also - how do you put a variable name in a
    > File.open("filename.txt") statement like this:
    > File.open(filevariable) where filevariable can
    > be set to anything? I could not find this in
    > the docs.
    >
    > Thanks, --Joe
    > ================ snip code ===============
    > #!/usr/bin/ruby
    >
    > F = "filename.txt"
    > searchstring = ARGV[0]
    >
    > print "searchstring is ", searchstring, "\n"
    >
    >
    > File.open("filename.txt").each { |line|
    > # this fails puts line if line =~ searchstring
    > # this fails puts line if line =~ /searchstring/
    > # this fails puts line if line =~ "searchstring"
    >
    > #this works
    > # puts line
    > }
    >
    >

    This should work, here is an image showing how to use it:
    http://tinypic.com/r/b5pi7m/6

    filename = 'source.txt'
    to_find = Regexp.new ARGV[0]

    puts "The Regexp is: #{to_find.inspect}"

    File.open(filename).each do |line|
    if line =~ to_find
    puts "This line matches: #{line}"
    puts "This is what matches: #{line[to_find]}"
    end
    end
     
    Josh Cheek, Jan 30, 2010
    #5
  6. Josh Cheek wrote:
    > File.open(filename).each do |line|
    > if line =~ to_find
    > puts "This line matches: #{line}"
    > puts "This is what matches: #{line[to_find]}"


    or to avoid having to match it twice:

    puts "This is what matches: #{$&}"

    Also:
    puts "This is what came before: #{$`}"
    puts "This is what came after: #{$'}"
    puts "Capture 1 is #{$1}"
    # etc
    --
    Posted via http://www.ruby-forum.com/.
     
    Brian Candler, Jan 30, 2010
    #6
  7. joemac

    joemac Guest

    On Jan 29, 7:38 pm, Harry Kakueki <> wrote:
    > > I dont know how to use avariablein a regex searchstring.
    > > ruby seems to only handle hardcoded serachstring in
    > > regex's.   How do you do this.   My code is below.

    >
    > Does this help?
    >
    > str = "abcdef"
    > searchstring = "cd"
    > p str =~ /#{searchstring}/
    >
    > Harry


    Hi Folks,

    Thanks for all the helpful info.
    It was the #{} syntax that i was missing.
    This also works for the "filename as a varible" situation
    when parsing lots of files:

    filenames = ["input1", "input2", "input3"]

    for fname in filenames
    infile = File.open("#{fname}")
    while line = infile.gets()
    puts line
    end
    puts "================== end file ==================="
    end


    Very helpfull!

    Thanks, --JM
     
    joemac, Feb 22, 2010
    #7
  8. On Feb 22, 2010, at 1:45 PM, joemac wrote:
    > On Jan 29, 7:38 pm, Harry Kakueki <> wrote:
    >>> I dont know how to use avariablein a regex searchstring.
    >>> ruby seems to only handle hardcoded serachstring in
    >>> regex's. How do you do this. My code is below.

    >>
    >> Does this help?
    >>
    >> str = "abcdef"
    >> searchstring = "cd"
    >> p str =~ /#{searchstring}/
    >>
    >> Harry

    >
    > Hi Folks,
    >
    > Thanks for all the helpful info.
    > It was the #{} syntax that i was missing.
    > This also works for the "filename as a varible" situation
    > when parsing lots of files:
    >
    > filenames = ["input1", "input2", "input3"]
    >
    > for fname in filenames
    > infile = File.open("#{fname}")
    > while line = infile.gets()
    > puts line
    > end
    > puts "================== end file ==================="
    > end
    >
    >
    > Very helpfull!
    >
    > Thanks, --JM


    In this case, you're already getting strings in fname so "#{fname}"
    isn't really doing anything useful.

    -Rob

    Rob Biedenharn http://agileconsultingllc.com
     
    Rob Biedenharn, Feb 22, 2010
    #8
  9. > In this case, you're already getting strings in fname so "#{fname}" isn't
    > really doing anything useful.


    Which means File.open(fname) would work directly.

    Cheers,

    --=20
    JJ Fleck
    PCSI1 Lyc=E9e Kl=E9ber
     
    Jean-Julien Fleck, Feb 22, 2010
    #9
    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. Anand Pillai

    String search vs regexp search

    Anand Pillai, Oct 12, 2003, in forum: Python
    Replies:
    10
    Views:
    620
    Anand Pillai
    Oct 15, 2003
  2. Paddy
    Replies:
    6
    Views:
    311
    Paddy
    Sep 15, 2007
  3. Abby Lee
    Replies:
    5
    Views:
    482
    Abby Lee
    Aug 2, 2004
  4. Mikel Lindsaar
    Replies:
    0
    Views:
    546
    Mikel Lindsaar
    Mar 31, 2008
  5. Joao Silva
    Replies:
    16
    Views:
    409
    7stud --
    Aug 21, 2009
Loading...

Share This Page