Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在perl中匹配字符串和整数_Perl_Matching - Fatal编程技术网

在perl中匹配字符串和整数

在perl中匹配字符串和整数,perl,matching,Perl,Matching,我有一串数字,比如“4 2 6 7”,还有一个变量I,它是一个整数。如何确定字符串中是否包含I?代码在perl中…使用此正则表达式将变量i与单词边界匹配(假设您的数字字符串在每个整数后面有一个空格): 使用此功能: my $string = "4 2 6 7"; my $i = 4; if ( $string =~ /\b$i\b/ ) { print "$string contains $i\n"; } 您可以使用split从字符串“4 2 6 7”创建一个数组,然后使用grep搜索

我有一串数字,比如“4 2 6 7”,还有一个变量
I
,它是一个整数。如何确定字符串中是否包含
I
?代码在perl中…

使用此正则表达式将变量i与单词边界匹配(假设您的数字字符串在每个整数后面有一个空格):

使用此功能:

my $string = "4 2 6 7";
my $i = 4;
if ( $string =~ /\b$i\b/ ) {
    print "$string contains $i\n";
}

您可以使用split从字符串“4 2 6 7”创建一个数组,然后使用grep搜索该数组

$ perl -wle 'if ( grep {$_ eq $i} split(" ", "4 2 6 7") ) {print "matched\n";}'
编辑:
或者,您可以使用“==”而不是“eq”作为比较运算符来匹配数字而不是字符串。

为了好玩,智能匹配运算符是:

use 5.012;
my $string = "4 2 6 7";
my @test = split /\s+/, $string;

for( 0 .. 9 ) {
    say "$_ is contained in $string" if $_ ~~ @test;
}

有关智能匹配运算符的功能的详细讨论,请参见。这可能有点棘手,因为它不是一个关联运算符,而且规则深深植根于DWIMery而不是一致性。但是它非常强大。

这里有一个版本不关心字符串的细节或格式。它只提取数字序列并将其与搜索模式进行比较

为了方便起见,我把它做成了一个子系统和一个功能程序

use warnings;
use strict;

my $string = "4 22 6 7";
my $i = shift; # number you want to search for
print "Checking '$string' for: '$i'\n";
print "Result is: ", (is_in($string, $i) ? "Yes" : "No");

sub is_in {
    my ($string, $i) = @_;
    while ( $string =~ /(\d+)/g ) {
        return 1 if ( $1 == $i );
    }
    return 0;
}
示例输出:

C:\perl>t4.pl 4
Checking '4 22 6 7' for: '4'
Result is: Yes
C:\perl>t4.pl 22
checking '4 22 6 7' for: '22'
Result is: Yes
C:\perl>t4.pl 2
checking '4 22 6 7' for: '2'
Result is: No

在函数的帮助下,你可以很容易地做到这一点

use warnings;

my $string = "4 2 6 7";
my $i = 6; #use any value of $i

my @x = split / /, $string;
my $count = 0;

foreach (@x)
{
     if($_ == $i) 
     { 
     print "matched at position $count"; die $!;
     }

$count++;
}
print "integer doesn't found in string";

在代码板上尝试:

如果字符串包含“3.0”且
i
为3,则您的代码将不匹配。更好的是Himadri Choudhury提供的split/grep解决方案。是的,它会,但在我为0时也会匹配。然而,“我是一个整数”让我想到“数字串”实际上是一个“整数串”是的
i
是一个整数,但他没有说字符串中的数字将是整数。他也没有说它们总是用空格分隔;逗号、分号甚至
?在某些情况下,你必须从表面上举例。是的,这是正确的,因为我只是在评论中添加了它作为“提示”。有时并非每个人都创建与应用程序匹配的示例。如果一个字符串包含数字,我认为它不太可能是浮点数。如果他或其他人阅读带有类似问题的问题,他已经从评论中得到了答案。“eq”是一个字符串比较。更好的是“=”,那么“3.0”和“3”也会匹配。“这可能是有人想要的东西。”希德。说得好。值得一提的是“情商”和“=”之间的区别。
use warnings;

my $string = "4 2 6 7";
my $i = 6; #use any value of $i

my @x = split / /, $string;
my $count = 0;

foreach (@x)
{
     if($_ == $i) 
     { 
     print "matched at position $count"; die $!;
     }

$count++;
}
print "integer doesn't found in string";