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.