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 `