为什么在脚本的第二个运行中,先显示"Not OK",然后显示"OK"?
Why in the second run of the script, it is first displayed "Not OK", and then displayed "OK"?
我想编写一个输出“OK”和“Not OK”的正则表达式,而不使用 if,else 结构:
validator_2.pl
:
#!/usr/bin/perl
use strict;
use utf8;
use locale;
use warnings;
use 5.10.0;
my $input = <STDIN>;
say "OK" if $input =~ m/^\+7\s\(\d{3}\)\s\d{3}-\d{2}-\d{2}$/ || say "Not OK";
$ echo "+7 (921) 123-45-67"|./validator_2.pl
OK
$ echo "+7 (921) 123-45-67888888"|./validator_2.pl
Not OK
OK
请研究下面的演示示例代码
注意:phone数字验证任务相当复杂,正则表达式不是用于此目的的正确方法
use strict;
use warnings;
use feature 'say';
my $re = qr/^\+7\s\(\d{3}\)\s\d{3}-\d{2}-\d{2}$/;
while( my $input = <DATA> ) {
chomp($input);
say "$input : ", ($input =~ /$re/) ? 'Ok' : 'Not OK';
}
__DATA__
+7 (921) 123-45-67
+7 (921) 123-45-67888888
输出
+7 (921) 123-45-67 : Ok
+7 (921) 123-45-67888888 : Not OK
我想编写一个输出“OK”和“Not OK”的正则表达式,而不使用 if,else 结构:
validator_2.pl
:
#!/usr/bin/perl
use strict;
use utf8;
use locale;
use warnings;
use 5.10.0;
my $input = <STDIN>;
say "OK" if $input =~ m/^\+7\s\(\d{3}\)\s\d{3}-\d{2}-\d{2}$/ || say "Not OK";
$ echo "+7 (921) 123-45-67"|./validator_2.pl
OK
$ echo "+7 (921) 123-45-67888888"|./validator_2.pl
Not OK
OK
请研究下面的演示示例代码
注意:phone数字验证任务相当复杂,正则表达式不是用于此目的的正确方法
use strict;
use warnings;
use feature 'say';
my $re = qr/^\+7\s\(\d{3}\)\s\d{3}-\d{2}-\d{2}$/;
while( my $input = <DATA> ) {
chomp($input);
say "$input : ", ($input =~ /$re/) ? 'Ok' : 'Not OK';
}
__DATA__
+7 (921) 123-45-67
+7 (921) 123-45-67888888
输出
+7 (921) 123-45-67 : Ok
+7 (921) 123-45-67888888 : Not OK