Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby:在字符串中插入一个字符?_Ruby - Fatal编程技术网

Ruby:在字符串中插入一个字符?

Ruby:在字符串中插入一个字符?,ruby,Ruby,我想在:00730中插入一个,,这样它就变成了:00730。我该怎么做 我在考虑 1 00:00:00000 -->00:00:00000 2 00:00:00730 -->00:00:04280 So when Sam originally sent me an email to do this course, 3 00:00:04280 -->00:00:08400 he said Ben can you teach a 50 minute course on m

我想在
:00730
中插入一个
,这样它就变成了
:00730
。我该怎么做

我在考虑

1
00:00:00000  -->00:00:00000
2
00:00:00730  -->00:00:04280
So when Sam originally sent me an email to do this course,  
3
00:00:04280  -->00:00:08400
he said Ben can you teach a 50 minute course on management.

但是我对正则表达式不是很熟悉,有没有更简单的方法呢?

使用正则表达式-捕获组和反向引用():


捕获组
(…)
可以用替换字符串引用
\1
\2
(引用第一个、第二个捕获组)

您也可以简单地运行

"00:00:04280  -->00:00:08400".gsub(/(\d{2})(\d{3})/, '\1,\2')
# => "00:00:04,280  -->00:00:08,400"
这将创建备份文件并进行所需的更改

"00:00:04280  -->00:00:08400".gsub(/(\d{2})(\d{3})/, '\1,\2')
# => "00:00:04,280  -->00:00:08,400"
`sed -i.bak 's/^\([[:digit:]]\{2\}:[[:digit:]]\{2\}:[[:digit:]]\{2\}\)\([[:digit:]]\{3\}  -->[[:digit:]]\{2\}:[[:digit:]]\{2\}:[[:digit:]]\{2\}\)\([[:digit:]]\{3\}\)$/\1,\2,\3/' lib/subtitle.txt `