简体   繁体   English

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

[英]Regex - split time duration from string

I currently have a string that is the title of a video. 我目前有一个字符串,它是视频的标题。 The string has annexed a time duration 00:00 . 该字符串附加了一个持续时间00:00 My regex currently is not splitting the tittle and duration. 我的正则表达式当前未拆分标题和持续时间。 How can accomplish this? 如何做到这一点?

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

Result: 结果:

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

Desired Result: 所需结果:

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

You need to put [az] inside the positive lookbehind and \\d inside the positive lookahead. 您需要将[az]放在正向后方, \\d放在正向后方。 Put \\s inbetween those assertions so that it would split your input according to the in-between space character. \\s置于这些断言之间,以便它将根据中间的空格字符分割输入。

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

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM