2013-04-17 16 views
-3

我有一個字符串變量$vreponse和字符串是如何檢查一些在字符串用perl

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

+0

你試過了什麼? –

+0

這是簡單的字符串搜索或正則表達式。 – DhruvPathak

+1

-1沒有研究工作'print(「some3text」=〜/ 3 /);'http://perldoc.perl.org/perlretut.html – Aprillion

回答

1

也許只是

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

這只是檢查是否存在一個3字符某處的字符串中。

或者更準確地說

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

其檢查最後字符是3,它是孤獨的,即不是,比方說,23結束。

+0

請找到我的代碼snipet – mac

+0

它正在使用〜/ 3 /而不是能夠使用〜/ \ b3 $/ – mac

+0

@mac:如果文本是從HTTPS請求返回的,那麼它不可能只是一行。 '/ \ b3 $ /'檢查*整個字符串*末尾是否有'3'。要檢查字符串中*行中是否有'3',可以使用'/ \ b3 $/m'。我無法比沒有看到數據的情況更好。 – Borodin

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 
} 
相關問題