正则表达式 - 从字符串拆分持续时间


Regex - split time duration from string

我目前有一个字符串,它是视频的标题。字符串附加了一个持续时间00:00 。我的正则表达式目前没有拆分标题和持续时间。如何做到这一点?

print_r(preg_split('#(?<='d)(?=[a-z])#', "The title of video 2:43"));

结果:

Array
(
    [0] => The title of video 2:43
)

期望的结果:

Array
(
    [0] => The title of video
    [1] => 2:43
)

你需要把[a-z]放在积极的展望中,'d放在积极的展望中。将's放在这些断言之间,以便它根据中间空格字符拆分您的输入。

print_r(preg_split('#(?<=[a-z])'s(?='d)#', "The title of video 2:43"));

为避免视频标题以数字结尾时过度匹配,您可以尝试使用以下代码:

print_r(preg_split('#(?<=[a-z])'s(?='d{1,2}':'d{2})#', "The title of video 2:43"));