ruby - What is the most efficient way to search for a sub-string -
in below string revision number 153. part inside string beside text "revision: 153\n".
string = "path: blah\blah\nworking copy root path: blah\blah\nurl: blah\blah\nrelative url: ^/readme.txt\nrepository root: blah\blah\nrepository uuid: 2d7234b632a-38239d-4384f-9ac3-c05286096652\nrevisio n: 153\nnode kind: file\nschedule: normal\nlast changed author: user_name\nlast changed rev: 153\nlast changed date: 2014-11-27 11:38:21 +0530 (thu, 27 nov 2014)\ntext last updated: 2014-11-27 12: 05:24 +0530 (thu, 27 nov 2014)\nchecksum: 7ecb9f79f244r4f996e637d6f23\n\n"
currently doing way :
string.split('revision').last.split('node').first.chomp.gsub(': ','').to_i
which outputs 153
.
is there better way it?
you can use string's []
, regular expression here:
string[/revision:\s(\d+)/, 1]
Comments
Post a Comment