1

I have an html file myfile.html, which includes a script with a line like this:

var json = '[{"name":"Hydrogen","number":"1","symbol":"H","weight":"1.00794"},{"name":"Helium","number":2,"symbol":"He","weight":4.002602},{"name":"Lithium","number":3,"symbol":"Li","weight":6.941},{"name":"Beryllium","number":4,"symbol":"Be","weight":9.012182},{"name":"Boron","number":5,"symbol":"B","weight":10.811},{"name":"Carbon","number":6,"symbol":"C","weight":12.0107}]';

The string within single quotes that is assigned to variable json will actually vary. I would like to replace this string with the entire contents of another file myjson.json.

I tried with the code here: Find and replace in a file in Ruby and here: search and replace with ruby regex doing this:

replace = File.read("myjson.json")
changefile = File.read("myfile.html")
changefile.sub( %r{var json = '[^<]+';}, replace )

but its not working. I'm not sure if its the regex I'm doing incorrectly, or if its something more.

UPDATE

After reading the reply below, my first attempt was:

replace = File.read("myjson.json")
changefile = File.read("myfile.html")
changefile.sub!(%r{var json = '.+'}, replace)
puts changefile

This did the find correctly, but removed all of the var json = '' and replaced it with myjson.json - I want to keep var json = and only replace the contents between the two single quotes after. So then I tried:

replace = File.read("myjson.json")
changefile = File.read("myfile.html")
changefile.sub!(%r{var json = '.+'}, "var json = 'replace'")
puts changefile

But that just replaced it with var json = 'replace'

I want to use the original var json = to find the location, but I don't want it to be removed.

So I did something I know is dumb and wrong, but it worked:

replace = File.read("myjson.json")
changefile = File.read("myfile.html")
changefile.sub!(%r{var json = '.+'}, "var json = 'thanksforthehelptinman'")
changefile.sub!(%r{thanksforthehelptinman}, replace)
puts changefile

Thanks for the help!

Community
  • 1
  • 1
majordomo
  • 1,160
  • 1
  • 15
  • 34

1 Answers1

0

The regex isn't right because [ and ] are reserved in regex. You need to escape them:

%r{var json = '\[.+\]'}

I can't be more exact because I don't know what's in your JSON file, but that should get you into the ballpark.

Also, unless you assign changefile.sub to something, the substitution will be thrown away. You can do one of these two things:

changefile = changefile.sub(%r{var json = '\[.+\]'}, json)

or mutate changefile:

changefile.sub!(%r{var json = '\[.+\]'}, json)
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
  • thanks for your help. I was using the [] b/c in the link above it was written, and I thought it was a reqd part of the regex. Again, the JSON above is similar to what needs to be replaced, but the exact contents of the var will be changing, as such the regex has to use wildcards to capture characters between the two single quotes. As far as the contents of the JSON in myjson.json, it too will constantly be changing, but it follows the same formatting as what is detailed above. – majordomo Jan 07 '13 at 06:51
  • Can't get it to work. How do I write the changed changefile back to myfile.html? – majordomo Jan 07 '13 at 07:00
  • That's a simple thing you can look up on your own. Look in Ruby's IO module. – the Tin Man Jan 07 '13 at 07:05
  • I was going to try this: `File.open("myfile.html","w") do |f| f.write(changefile) end` – majordomo Jan 07 '13 at 07:13