如何重新排序字符串中的子字符串? [英] How can I reorder substrings in a string?

查看:42
本文介绍了如何重新排序字符串中的子字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 Perl 的正则表达式中进行以下转换?

How do I do the following conversion in regex in Perl?

British style   US style
"2009-27-02" => "2009-02-27"

我是 Perl 的新手,对正则表达式知之甚少,我能想到的就是提取-"的不同部分,然后重新连接字符串,因为我需要即时进行转换,我觉得我的方法会非常缓慢和丑陋.

I am new to Perl and don't know much about regex, all I can think of is to extract different parts of the "-" then re-concatenate the string, since I need to do the conversion on the fly, I felt my approach will be pretty slow and ugly.

推荐答案

use strict;
use warnings;
use v5.10;

my $date = "2009-27-02";
$date =~ s/(\d{4})-(\d{2})-(\d{2})/$1-$3-$2/;
say $date;

这篇关于如何重新排序字符串中的子字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆