简体   繁体   English

Perl Regex发现值太小并附加

[英]Perl Regex finding value too small and appending

I am working on an assignment which requires me to read in a bunch of names and phonebook and essentially create a search that works much like the dialer on your phone. 我正在做一项作业,要求我读一堆名称和电话簿,并且本质上创建一个搜索,该搜索的工作方式与您手机上的拨号程序非常相似。 If a number only consists of xxx-xxxx then an area code should be appended. 如果数字仅由xxx-xxxx组成,则应附加区号。 I am successfully able to pick out strings that consist of xxx-xxxx but when I try to print that string I just get an empty line. 我能够成功地挑选出包含xxx-xxxx的字符串,但是当我尝试打印该字符串时,我只会得到一个空行。 When I call the previous index, it successful prints out the name. 当我调用上一个索引时,它会成功打印出名称。

foreach my $i (0 .. $#phoneBook) {
    my @splitIndex = split(':', $phoneBook[$i]); #index 0 and 1 of splitIndex
    #If the length of the number is too short append the 701 before
    if($splitIndex[1] =~ s/^(\d{3})-(\d{4})//) {
      print "$splitIndex[1]\n";
    }
    foreach my $j (0 .. $#splitIndex) {
      #print "$splitIndex[$j]";
    }
    if(($splitIndex[0] =~ /$searchValue/i) || ($splitIndex[1] =~ /$searchValue/i)) {
      #print "Found\n";
    } else {
      #print "Not Found $searchValue\n";
    }
  }

In my file, the text John:888-8888 is saved in my phoneBook array. 在我的文件中,文本John:888-8888保存在我的电话簿数组中。 But when I call splitIndex[1] (the phone number) I get an empty line. 但是,当我致电splitIndex [1](电话号码)时,我得到了一个空行。

The issue was I was using the substitute rather than the match tag in the line. 问题是我在一行中使用替代标签而不是match标签。

$splitIndex[1] =~ s/^(\d{3})-(\d{4})//

To

$splitIndex[1] =~ m/^(\d{3})-(\d{4})/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM