使用 perl 在字符串中搜索未知子字符串 [英] Searching a unknown substring in a string using perl
问题描述
我正在尝试从网站下载文件.但我只想在文件版本比我拥有的文件版本最新时下载.
Im trying to download a file from a website. But i want to download only if the file version is latest than the same file I have.
文件名为 CTP-Latest5.0.37.iso
The file name is CTP-Latest5.0.37.iso
我需要检查文件名中的字符串37".
I need to check the string "37" in the file name.
因此,如果数字 37 大于我已经拥有的版本,那么只有我下载.
So if the number 37 is greater than the version Im already having then only I download.
下载文件后,我想存储字符串37"是一个文件,以便下次运行 perl 代码检查版本时可以读取它
Once I download the file I want to store the string "37" is a file so that I can read it next time when I run the perl code to check for version
这是我尝试使用的代码
#!/usr/bin/perl
use LWP::Simple;
#my $version =37;//this whole line is commented
Open file version.txt and get the version number(say 37)and store it in $version
$pageURL="http://www.google.comisos/psfsS5.3/LATIMGODCVP/";
$simplePage=get($pageURL);
Now $simplePage has the substring "CTP-LATEST-5.3.0.(some_version_number).iso"
I need to get that "some_version_number". HOW ?
if(some_version_numner > $version)
{
#-- fetch the zip and save it as perlhowto.zip
my $status = getstore("http://www.google.com/isos/psfsS5.3/LATIMGODCVP/CVP-LATEST-5.3.0."$version".iso", "CTP-Latest5.3.0.$version.iso");
if ( is_success($status) )
{
print "file downloaded correctly\n";
Store the "some_version_number in file version.txt (for next time use)
}
else
{
print "error downloading file: $status\n";
}
}
我希望代码中的一切都清楚.version.txt 只存储了 1 个任意数字的字符串(比如 37 或 45 或 89 等)
I hope everything is clear in code. The version.txt has only 1 string stored that is any number (say 37 or 45 or 89 etc )
有人可以帮我完成其余的代码吗?
Can someone help me on how to do the rest of the code ?
推荐答案
如果 some_version_number
只是数字,你可以这样做:
If some_version_number
is only digits, you can do:
if ($simplePage =~ /CTP-LATEST-5\.3\.0\.\((\d*)\)\.iso/)
{
$some_version_number = $1;
}
如果不仅是数字,而且simplePage
中没有其他括号,你可以这样做:
If not only digits, but there are no other parentheses in simplePage
, you can do:
if ($simplePage =~ /CTP-LATEST-5\.3\.0\.\((.*)\)\.iso/)
{
$some_version_number = $1;
}
这篇关于使用 perl 在字符串中搜索未知子字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!