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

时间:2015-03-11 15:46:45

标签: php regex

我目前有一个字符串是视频的标题。该字符串附加了一段时间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
)

2 个答案:

答案 0 :(得分:1)

你需要将[a-z]放在积极的前瞻内,\d放在积极的前瞻内。将\s置于这些断言之间,以便根据中间空格字符分割输入。

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

答案 1 :(得分:1)

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

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