首页 > 解决方案 > 在 perl 中使用正则表达式并且它不能正常工作

问题描述

这是我第一次尝试使用正则表达式,也是我第一次尝试使用 perl,所以这是一场斗争。我正在编写一个循环来查看一个成像的 USB 并用它提取里面的所有电话号码

my $phonefilename = 'phoneoutput.txt';
open(FFF, "<usb256.001");
open(FH, '>', $phonefilename) or die $!;
my $phonenumber;
while(<FFF>)
{
    if (/^\+[[:space:]]*[0-9][0-9.[:space:]-]*(\([0-9.[:space:]-]*[0-9][0-9.[:space:]-]*\))?([0-9.[:space:]-]*[0-9][0-9.[:space:]-]*)?([[:space:]]+ext.[0-9.[:space:]-]*[0-9][0-9.[:space:]-]*)?/i)
    {
        $phonenumber = $1;
        print FH "$phonenumber\n";
    }
}
close(FFF);

并且我在 datafinder.pl 第 29 行不断收到 Use of uninitialized value $phonenumber in concatenation (.) 或 string,有什么帮助吗?

标签: regexperl

解决方案


只是为了好玩,假设我们处理由 11 位数字组成的北美号码

  • pos1: 1 - 北美
  • pos2: 2-4 - 区号
  • 位置 3:5-7 - 第 1 组
  • 位置 4:8-11 - 第 2 组

代码片段:验证是在验证数字仅包含 11 位数字时实现的

use strict;
use warnings;
use feature 'say';

my %number;                                         # has to store number

while(my $data = <DATA>) {                          # walk through data
    chomp $data;                                    # snip eol
    $data =~ tr/0-9//cd;                            # remove spaces, brakets, dashes
    if( length($data) != 11 ) {                     # is number consist of 11 digits?
        say '-' x 40 . "\n"                         #   no  = invalid
          . "Number:  $data is invalid";
    } else {                                        #   yes = valid
        $data =~ /(\d)(\d{3})(\d{3})(\d{4})/;       #   regex and capture
        @number{qw/country area group1 group2/} = ($1,$2,$3,$4);    # store
        say '-' x 40 . "\n"
          . "Belongs to "
          . ($number{country} == 1 ? "North America" : "None North America");
        say "Number:  $data\n"                      #   output
          . "Record:  $number{country} ($number{area}) $number{group1}-$number{group2}\n"
          . "Country: $number{country}\n"
          . "Area:    $number{area}\n"
          . "Group1:  $number{group1}\n"
          . "Group2:  $number{group2}";
    }
}

__DATA__
1 (309) 123-4567
1 312-123-4567
1 (815) 123-456
3 (421) 123-4567
3 (426) 123-4567
17731234567
1872123456

输出

----------------------------------------
Belongs to North America
Number:  13091234567
Record:  1 (309) 123-4567
Country: 1
Area:    309
Group1:  123
Group2:  4567
----------------------------------------
Belongs to North America
Number:  13121234567
Record:  1 (312) 123-4567
Country: 1
Area:    312
Group1:  123
Group2:  4567
----------------------------------------
Number:  1815123456 is invalid
----------------------------------------
Belongs to None North America
Number:  34211234567
Record:  3 (421) 123-4567
Country: 3
Area:    421
Group1:  123
Group2:  4567
----------------------------------------
Belongs to None North America
Number:  34261234567
Record:  3 (426) 123-4567
Country: 3
Area:    426
Group1:  123
Group2:  4567
----------------------------------------
Belongs to North America
Number:  17731234567
Record:  1 (773) 123-4567
Country: 1
Area:    773
Group1:  123
Group2:  4567
----------------------------------------
Number:  1872123456 is invalid

推荐阅读