0
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?

1 Answer 1

2

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)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.