从另一个获取变量的值

时间:2013-01-22 21:03:21

标签: perl

你能协助我确定正确的$ string =行,最终得到包含4165867111的partial_phone吗?

sub phoneno {
my ($string) = @_;
$string =~  s/^\+*0*1*//g;
return $string;
}

my $phone = "<sip:+4165867111@something;tag=somethingelse>";

my $partial_phone = phoneno($phone);

3 个答案:

答案 0 :(得分:3)

$string =~ s{
    \A          # beginning of string
    .+          # any characters
    \+          # literal +
    (           # begin capture to $1
        \d{5,}  # at least five digits
    )           # end capture to $`
    \@          # literal @
    .+          # any characters
    \z          # end of string
}{$1}xms;

答案 1 :(得分:2)

您的替换以^开头,这意味着除非您的模式的其余部分与字符串的开头匹配,否则它不会执行替换。

有很多方法可以做到这一点。 <怎么样

my ($partial) = $phone =~ /([2-9]\d+)/;
return $partial;

返回任何不以0或1开头的数字字符串。

答案 2 :(得分:2)

这将捕获@之前的所有数字:

use strict;
use warnings;

sub phoneno {
    my ($string) = @_;
    my ($phoneNo) = $string =~ /(\d+)\@/;
    return $phoneNo;
}

my $phone = '<sip:+4165867111@something;tag=somethingelse>';

my $partial_phone = phoneno($phone);

print $partial_phone;

输出:

4165867111