I am trying to make a UUID into a properly conformed UUID by inserting a hyphen within each section of the substring.
test = "CB13DBB20A9945CC86F11914C979C761"
#The first one will return '----' so essentially the $1 to $5 are returned as emptys
test.sub(/(\h{8})(\h{4})(\h{4})(\h{4})(\h{12})/, "#{$1}-#{$2}-#{$3}-#{$4}-#{$5}")
#Returns the ideal result of CB13DBB2-0A99-45CC-86F1-1914C979C761
test.sub(/(\h{8})(\h{4})(\h{4})(\h{4})(\h{12})/, "#{$1}-#{$2}-#{$3}-#{$4}-#{$5}")
As you can see, the first run of the function does not work, but the second does. Any ideas would be great. As additional information,
test.match(/(\h{8})(\h{4})(\h{4})(\h{4})(\h{12})/){|m| "#{$1}-#{$2}-#{$3}-#{$4}-#{$5}"}
will work on the first time instead. Single quotes and double quotes do not affect anything.
Reference the pattern matches like this.
The reason you are seeing this behaviour is explained in the Ruby Docs: