I need to convert timestamp of the format 'h:m:s' to '0h:0m:0s' . ie if any of hour or minute or second is single digit then add leading zero.
example-
1:6 should be converted to 01:06
1:5:6 should be converted to 01:05:06
05:6 should be converted to 05:06
What i tried?
$this_video_duration =~ s/(^|:)(\d(:|$))/0$2/g;
Please help me for this regex.
$this_video_duration =~ s/(\d+)/ length($1) >1 ? $1 : "0$1" /ge;
$x = join ':', map { sprintf '%02d', $_ } split /:/, $x;
要么
$x =~ s/\d+/ sprintf '%02d', $_ /eg;
Your issue is that the ending :
in a first match is "eating" the beginning :
of the next match.
You could use zero width word boundary:
$this_video_duration =~ s/\b\d\b/0$&/g;
\\b
will match between \\w\\W
or \\W\\w
, and \\d
is in \\w
. Demo here .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.