如何使用perl检查字符串中的数字

时间:2013-04-17 10:19:37

标签: regex perl

我在变量$vreponse中有一个字符串,字符串是

int.force_snmp_version  T_SIZE  3

我想要做的就是验证字符串中是否有数字3。如果验证成功则打印消息或打印失败消息

我尝试过这样的事情

my $vresponse = $ua->get("https://$user:$pass\@$ern_ip/get_param?p=init.force_snmp_version");

if ($vresponse->decoded_content =~ /\b3$/)
{
print "SUCESS\n";
}
else
{ print "not\n"; }

这不起作用,我是否需要更改$vresponse->decoded_content

2 个答案:

答案 0 :(得分:1)

也许只是

if ( $vresponse =~ /3/ ) { ... }

只检查字符串中某处有3个字符。

或者,更确切地说是

if ( $vresponse =~ /\b3$/ ) { ... }

检查 last 字符是否为3并且它是单独的,即不是23的结尾。

答案 1 :(得分:0)

my $vresponse = 'int.force_snmp_version  T_SIZE  3';
my $char = '3';

my $result = index($vresponse , $char);

if ($result >=0)
{
  #display found
}
else
{
  #display not found
}