Reputation: 313
So this would be my Sample:
In Kenya since 2010 <a href="https://www.youtube.com/watch?v=fDTGpMJi0OI&list=PLqGkpApxFsX8k8ENdCr-9v8iRa2q5jXaE&" onclick="yt.www.watch.player.seekTo(2*60+48);return false;">2:48</a>
Where comes from: Bukavu <a href="https://www.youtube.com/watch?v=fDTGpMJi0OI&list=PLqGkpApxFsX8k8ENdCr-9v8iRa2q5jXaE&" onclick="yt.www.watch.player.seekTo(3*60+29);return false;">3:29</a>
She would have been killed <a href="https://www.youtube.com/watch?v=fDTGpMJi0OI&list=PLqGkpApxFsX8k8ENdCr-9v8iRa2q5jXaE&" onclick="yt.www.watch.player.seekTo(5*60+15);return false;">5:15</a>
now the times m:ss (like 2:48) should stay where they are, but ALSO be appended to the link that comes before it, after the "&" and in the following format: 2:48 should become t=2m48s
So, at the end, the whole thing should look like this (check the end of the links)
In Kenya since 2010 <a href="https://www.youtube.com/watch?v=fDTGpMJi0OI&list=PLqGkpApxFsX8k8ENdCr-9v8iRa2q5jXaE&t=2m48s" onclick="yt.www.watch.player.seekTo(2*60+48);return false;">2:48</a>
Where comes from: Bukavu <a href="https://www.youtube.com/watch?v=fDTGpMJi0OI&list=PLqGkpApxFsX8k8ENdCr-9v8iRa2q5jXaE&t=3m29s" onclick="yt.www.watch.player.seekTo(3*60+29);return false;">3:29</a>
She would have been killed <a href="https://www.youtube.com/watch?v=fDTGpMJi0OI&list=PLqGkpApxFsX8k8ENdCr-9v8iRa2q5jXaE&t=5m15s" onclick="yt.www.watch.player.seekTo(5*60+15);return false;">5:15</a>
Now I found out, i could use \d:.. to find the times, but I have no Idea on how to proceed or if it is possible at all with sublime text ?
Upvotes: 0
Views: 728
Reputation: 20486
Update 2:
I swear I checked on YouTube, and I was able to go to a video with &t=h5m10s
and it would work as 0:05:10
. If you need this to be 5m10s
or 0h5m10s
, you'll need to do two different expressions (since Sublime Text won't let you do complex callback functions on your regex replace).
To replace h:mm:ss
:
href="(.*?)"(.*?)>(\d*):(\d{2}):(\d{2})
href="$1&t=$3h$4m$5s"$2>$3:$4:$5
To replace m:ss
:
href="(.*?)"(.*?)>(\d{1,2}):(\d{2})(?!:\d)
href="$1&t=$3m$4s"$2>$3:$4
The only unusual syntax that needs explanation is (?!:\d)
. The ?!
syntax is for a "negative lookahead". This means that the match group will not be followed by an additional :\d
, otherwise this would match hours/minutes/seconds.
Update:
Here is an updated expression for matching optional hours. I double checked and you are able to use &t=h##m##s
(i.e., a blank hour if it is not matched). This means we can do one search/replace and just have a blank capture for the hour if it does not exist (you'll see what I mean in a second).
Match this:
href="(.*?)"(.*?)>((?:(\d*):)?(\d{1,2}):(\d{2}))
And replace with:
href="$1&t=$4h$5m$6s"$2>$3
Things got a little more complicated here. Lets take a closer look at the part that changes:
( # start an overarching capture group
(\d*):? # capture 0+ digits followed by an optional : (hours)
(\d{1,2}): # capture 1-2 digits followed by a : (minutes)
(\d{2}) # capture the last 2 digits (seconds)
) # end the capture group
Now you may wonder why I used that overarching capture group. This is because we need a way to keep the contents of the anchor link the same. If we didn't use this, we would have to replace it with hour:minute:second
and hour may be blank (meaning 3:45
would be replaced with :3:45
).
You can match this:
href="(.*?)"(.*?)>(\d)+:(\d{2})
And replace it with
href="$1&t=$3m$4s"$2>$3:$4
How this works is it first finds any anchor's href, while capturing the contents in the first capture group. Then we capture everything up to the >
in a second capture group. Next we capture 1+ digit (third capture), followed by :
, and finally capturing the last 2 digits (fourth capture).
We essentially replace this with all the same content, but make sure to append &t=$3m$4s
to the end of the link. You can see a working example on Regex101.
Upvotes: 1