Newbie Regexp question

Moi,
I’m new to ruby and even to regular expression, so I fear these might be
super simple. 2 Questions

I have a string (actually a java inner class file name) like

name = “com.foo.Outer$Inner.class”

and want to replace $ with $ (for the shell)

name.sub!(/$/,“\$”)
doesn’t work while
name[$/] = “\$”
does.

And the other one is with a line (from a config file) like
line = "option.name = “value 1” “value 2” "

and I’d like an expression that
1 gets me the stuff after the = , and whatever I do the = is always part
of the result
2 an expession that I could use with split that gets an array [
“option.name”,“value 1” , “value 2”]

Thanks
Torsten

name.sub!(/\$/,"\\\$")

It's best to use ''

Tpigeon% ruby
name = "com.foo.Outer$Inner.class"
name.sub!(/\$/, '\\$')
p name
^D
"com.foo.Outer\\$Inner.class"
pigeon%

line = "option.name = "value 1" "value 2" "

Well, you can write

pigeon% ruby
line = 'option.name = "value 1" "value 2"'
if /=(.*)/ =~ line
   p $1
end

if /=/ =~ line
   p $'
end
^D
" \"value 1\" \"value 2\""
" \"value 1\" \"value 2\""
pigeon%

2 an expession that I could use with split that gets an array [
"option.name","value 1" , "value 2"]

Well, the problem is that you have space between "" and #split is not
really adapted for this

You can do

pigeon% ruby
line = 'option.name = "value 1" "value 2"'
if /\A[\w.]+\s*=\s*".*"/ =~ line && array = line.split(/\s*=\s*/, 2)
   array[1] = array[1].scan(/"([^"]*)"/)
   array.flatten!
else
   array =
end
p array
^D
["option.name", "value 1", "value 2"]
pigeon%

Guy Decoux

Moi,
I’m new to ruby and even to regular expression, so I fear these might be
super simple. 2 Questions

I have a string (actually a java inner class file name) like

name = “com.foo.Outer$Inner.class”

and want to replace $ with $ (for the shell)

name.gsub(/$/, ‘$’)

And the other one is with a line (from a config file) like
line = "option.name = “value 1” “value 2” "

and I’d like an expression that
1 gets me the stuff after the = , and whatever I do the = is always part
of the result
2 an expession that I could use with split that gets an array [
“option.name”,“value 1” , “value 2”]

  1. value = /=.*$/.match(line)[0]
    (see ri Regexp.match and ri MatchData)

  2. value.scan(/“(.*?)”/).flatten → [“value 1”, “value 2”]

Thanks
Torsten

Regards,
Gavin

···

----- Original Message -----
From: “Torsten Rüger” torsten.rueger@firsthop.com