在perl中匹配字符串和整数

发布于 2024-11-19 04:42:40 字数 94 浏览 1 评论 0原文

我有一串数字,例如“4 2 6 7”,还有一个整数变量 i 。如何确定 i 是否包含在字符串中?代码是用perl写的...

I have a string of numbers, like "4 2 6 7", and a variable i which is an integer. How can I decide if i is included in the string? The code is in perl...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

痞味浪人 2024-11-26 04:42:40

使用这个函数:

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

Use this function:

my $string = "4 2 6 7";
my $i = 4;
if ( $string =~ /\b$i\b/ ) {
    print "$string contains $i\n";
}
别忘他 2024-11-26 04:42:40

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

$ perl -wle 'if ( grep {$_ eq $i} split(" ", "4 2 6 7") ) {print "matched\n";}'

编辑:
或者您可以使用“==”而不是“eq”作为比较运算符来匹配数字而不是字符串。

You can use split to create an array from the string "4 2 6 7", and then use grep to search the array.

$ perl -wle 'if ( grep {$_ eq $i} split(" ", "4 2 6 7") ) {print "matched\n";}'

EDIT:
Or you can use '==' instead of 'eq' as the comparison operator to match numbers instead of strings.

与往事干杯 2024-11-26 04:42:40

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

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

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

关于智能匹配运算符的强大功能的精彩讨论可以在 perlsyn。它可能有点棘手,因为它不是关联运算符,并且规则深深植根于 DWIMery 而不是一致性。但它非常强大。

For the fun of it, the ~~ smart match operator:

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

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

A good discussion on the power of the smart match operator is found in perlsyn. It can be a little tricky, since it's not an associative operator, and the rules are deeply rooted in DWIMery rather than consistency. But it's very powerful.

羁〃客ぐ 2024-11-26 04:42:40

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

/\b$i\b/

Use this regular expression to match the variable i with a word boundary (assuming your string of numbers have a space after each integer):

/\b$i\b/
绮烟 2024-11-26 04:42:40

这是一个不关心字符串的分隔符或格式的版本。它只是提取数字序列并将它们与搜索模式进行比较。

为了方便起见,我把它做成了一个子程序和一个函数程序。

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

Here's a version that does not care about the delimeters or formatting of your string. It just extracts sequences of digits and compares them to the search pattern.

I made it into a sub and a functional program, for convenience.

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;
}

Example output:

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
栀子花开つ 2024-11-26 04:42:40

您可以借助 split 函数轻松完成此操作。

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";

在键盘上尝试一下:http://codepad.org/f5a86c9s

You can do it easily with the help of split function.

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";

Try it on codepad: http://codepad.org/f5a86c9s

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文