The following code is a line in an xml file:
<appId>455360226</appId>
How can I replace the number between the 2 tags with another number using ruby?
The following code is a line in an xml file:
<appId>455360226</appId>
How can I replace the number between the 2 tags with another number using ruby?
You can do it in one line like this:
IO.write
truncates the given file by default, so if you read the text first, perform the regexString.gsub
and return the resulting string usingFile.open
in block mode, it will replace the file's content in one fell swoop.I like the way this reads, but it can be written in multiple lines too of course:
There is no possibility to modify a file content in one step (at least none I know, when the file size would change). You have to read the file and store the modified text in another file.
Or you read the file content to memory and afterwords you overwrite the file with the modified content:
(Hope it works, the code is not tested)
The right way is to use an XML parsing tool, and example of which is XmlSimple.
You did tag your question with regex. If you really must do it with a regex then
is an illustration of the kind of thing you can do but shouldn't.