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.
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"
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}/
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
···
On Fri, Jan 29, 2010 at 6:50 PM, joemac <joemacbusiness@gmail.com> 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.
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"
On Jan 29, 10:38 pm, Harry Kakueki <list.p...@gmail.com> 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}/
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
···
On Jan 29, 7:38 pm, Harry Kakueki <list.p...@gmail.com> 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}/
On Jan 29, 10:38�pm, Harry Kakueki <list.p...@gmail.com> 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).
On Jan 29, 7:38 pm, Harry Kakueki <list.p...@gmail.com> 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