我正在尝试通过在子字符串的每个部分中插入一个连字符来将 UUID 变成一个正确符合的 UUID。
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}")
如您所见,该函数的第一次运行不起作用,但第二次运行。任何想法都会很棒。作为附加信息,
test.match(/(\h{8})(\h{4})(\h{4})(\h{4})(\h{12})/){|m| "#{$1}-#{$2}-#{$3}-#{$4}-#{$5}"}
将在第一次工作。单引号和双引号不影响任何东西。