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.
I want to insert a ,
into :00730
, so it becomes :00,730
. How can I do that?
I'm thinking about
path = 'lib/subtitle.txt'
lines = IO.readlines(path).map do |line|
*if contains 5 number, then insert a comma into it, like `gsub?`
end
File.open(path, 'w') do |file|
file.puts lines
end
But I'm not very familiar with Regex, is there a simpler way of doing this?
Using regular expression - capturing group and backreference ( String#gsub
):
"00:00:04280 -->00:00:08400".gsub(/(\d{2})(\d{3})/, '\1,\2')
# => "00:00:04,280 -->00:00:08,400"
capturing groups (...)
can be referenced in replacement string with \\1
, \\2
(reference first, second captured group)
You also can simply run
`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 `
This creates backup file and makes changes you need.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.