0

你能帮我确定正确的 $string = line 以包含 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);
4

3 回答 3

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;
于 2013-01-22T21:12:19.720 回答
2

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

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

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

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

于 2013-01-22T21:10:04.063 回答
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
于 2013-01-22T21:27:39.583 回答