string – 如何在Perl中将人的全名解析为用户名?

前端之家收集整理的这篇文章主要介绍了string – 如何在Perl中将人的全名解析为用户名?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我需要将Parisi,Kenneth格式的名称转换为kparisi格式.

有谁知道如何在Perl中这样做?

以下是一些异常的示例数据:

Zelleb,Charles F.,IV
埃尔特,约翰,四
Wods,Charles R.,III
Welkt,Craig P.,Jr.

这些特定的名称最终应该是czelleb,jeilt,cwoods,cwelkt等.

我还有一个条件毁了我的名字建设者
O’Neil,Paulso far,Vinko Vrsalovic的答案是在奇怪/腐败的名字在混合中时发挥最佳作用,但上面这个例子会出现“pneil”…如果我不能在那之间得到那个,那么我会被诅咒p和n

解决方法

vinko@parrot:~$cat genlogname.pl
use strict;
use warnings;

my @list;
push @list,"Zelleb,IV";
push @list,"Eilt,John,"Woods,III";
push @list,"Welkt,Jr.";

for my $name (@list) {
        print gen_logname($name)."\n";
}

sub gen_logname {
        my $n = shift;
        #Filter out unneeded characters
        $n =~ s/['-]//g;
        #This regex will grab the lastname a comma,optionally a space (the 
        #optional space is my addition) and the first char of the name,#which seems to satisfy your condition
        $n =~ m/(\w+),?(.)/;
        return lc($2.$1);
}
vinko@parrot:~$perl genlogname.pl
czelleb
jeilt
cwoods
cwelkt

猜你在找的Perl相关文章