0

我对正则表达式的概念相当陌生。我了解我在 bash 脚本中使用的基本正则表达式。以下代码片段来自我正在编写的用于自动更新服务器上的 Wordpress 插件的程序。

无论如何,这个概念是这段代码是子程序的一部分,它通过目录中的 .php 文件递归,并尝试从文件,如果模式匹配,则另一个子尝试提取字符“:”后面的值以获取正确的版本号。

$searchpath=$path."/".$plugins[$i];
        @files = <$searchpath/*.php>;
        print "Search path is ".$searchpath."\n";
OUT:    foreach $file (@files) 
        {
            print "Checking alternate php file: ".$file."\n";
            open(txt, $file);
            while($line = <txt>) 
            {
                for ($line)
                {
                 s/^\s+//;
                 s/\s+$//;
                }
                if ( $line =~ /^Version:|^version:|^\* Version:|\sVersion:/ )
                {
                    print "Version found in file ".$file."\n";  
                    $varfound=1;    
                    close(txt);
                    $ver=&read_extract($file);
                    print $ver."\n";
                    $pluginversion[$i]=$ver;
                    print "Array Num ".$i." Stored plugin name:".$plugins[$i]." Version found ".$ver." Version stored ".$pluginversion[$i]."\n";
                    last OUT;
                }
            }
        }

问题是我似乎在逻辑上有错误,并且该文件实际上与 " . phpversion() . "\n"; Version stored " 匹配。php版本()。"\n" 用于搜索查询。以我有限的知识,我发现很难理解哪里出了问题,并且渴望得到一些建议。

提及的其他子项包括如下:

sub read_extract
{
    my $pl_version="";
    open(txt, my $file=$_[0]);
    while($line = <txt>)
    {
        for ($line)
        {
         s/^\s+//;
         s/\s+$//;
        }           
        if ( $line =~ /^Version:|^version:|^\* Version:|\sVersion:/ )
        {
            $pl_version=&extract_version($line);
        }
    }
    close(txt);
    $pl_version;
}

sub extract_version
{
    my $line=$_[0];
    $string=substr($line,rindex($line, ":")+1);
    for ($string)
    {
     s/^\s+//;
     s/\s+$//;
    }
    $string;
}

如果完全需要我的子程序,我可以包含它。但是我的调试行显示了这一点:

Processing xcloner-backup-and-restore...Search path is /var/www/virtual/joel.co.in/vettathu.com/htdocs/wp-content/plugins/xcloner-backup-and-restore
Checking alternate php file: /var/www/virtual/joel.co.in/vettathu.com/htdocs/wp-content/plugins/xcloner-backup-and-restore/admin.cloner.html.php
Checking alternate php file: /var/www/virtual/joel.co.in/vettathu.com/htdocs/wp-content/plugins/xcloner-backup-and-restore/admin.cloner.php
Checking alternate php file: /var/www/virtual/joel.co.in/vettathu.com/htdocs/wp-content/plugins/xcloner-backup-and-restore/admin.xcloner-backupandrestore.php
Checking alternate php file: /var/www/virtual/joel.co.in/vettathu.com/htdocs/wp-content/plugins/xcloner-backup-and-restore/admin.xcloner.php
Checking alternate php file: /var/www/virtual/joel.co.in/vettathu.com/htdocs/wp-content/plugins/xcloner-backup-and-restore/cloner.config.php
Checking alternate php file: /var/www/virtual/joel.co.in/vettathu.com/htdocs/wp-content/plugins/xcloner-backup-and-restore/cloner.cron.php
Checking alternate php file: /var/www/virtual/joel.co.in/vettathu.com/htdocs/wp-content/plugins/xcloner-backup-and-restore/cloner.functions.php
Version found in file /var/www/virtual/joel.co.in/vettathu.com/htdocs/wp-content/plugins/xcloner-backup-and-restore/cloner.functions.php
" . phpversion() . "\n";
Array Num 26 Stored plugin name:xcloner-backup-and-restore Version found " . phpversion() . "\n"; Version stored " . phpversion() . "\n";

这似乎是错误所在。

4

1 回答 1

1

好吧,那里有很多冗余代码。如果您已经有了该行,为什么需要关闭文件并再次找到该行?您需要做的就是在找到该行时捕获该字符串:

if ( $line =~ /^\*?\s?Version:(.*)/i ) {
    my $version = $1;

因此,通过使用/i修饰符,您的匹配不区分大小写。通过放在?后面\*\s他们可以匹配 0 次或 1 次。通过使用(.*)该行的其余部分被捕获到$1.

您的正则表达式在最后一场比赛中缺少^行首锚点,我认为这是一个错字。如果没有,您可以简单地将正则表达式更改为/\bVersion:(.*)/i. 并且\b仅对避免部分匹配有用,例如subversion: foo.

于 2013-03-31T11:06:46.170 回答