简体   繁体   English

Perl-数组中正则表达式的异常行为

[英]Perl - Unexptected behaviour with Regex in Array

I'm trying to match lines that have 我正在尝试匹配具有

"/foldera/folderb/folderc/folderd/file.ext@@/main" + "/" + ANY_NUMBER:

so for example: 因此,例如:

(.+)(main)(.\d)

The lines: 这些行:

/foldera/folderb/folderc/folderd/file.ext@@/main
/foldera/folderb/folderc/folderd/file.ext@@/main/0
/foldera/folderb/folderc/folderd/file.ext@@/main/1
/foldera/folderb/folderc/folderd/file.ext@@/main/2
/foldera/folderb/folderc/folderd/file.ext@@/main/3
/foldera/folderb/folderc/folderd/file.ext@@/main/4
/foldera/folderb/folderc/folderd/file.ext@@/main/5 (RLT-abcde, BLD-abcde, DEV-abcde)
/foldera/folderb/folderc/folderx/file12.ext@@/main/0
/foldera/folderb/folderc/folderx/file12.ext@@/main/1
/foldera/folderb/folderc/folderx/file12.ext@@/main/2
/foldera/folderb/folderc/folderx/file12.ext@@/main/3
/foldera/folderb/folderc/folderx/file12.ext@@/main/4
/foldera/folderb/folderc/folderx/file12.ext@@/main/5
/foldera/folderb/folderc/folderx/file12.ext@@/main/6 (RLS-abcde-5.0, RLS-abcde-4.1)

While my regex matches the desired lines (I checked it at http://www.regexe.com/ ), in my Perl program it does not match 虽然我的正则表达式与所需的行匹配(我在http://www.regexe.com/上进行了检查),但在我的Perl程序中却不匹配

/foldera/folderb/folderc/folderd/file.ext@@/main

but it does match: 但它确实匹配:

/foldera/folderb/folderc/folderd/file.ext@@/main/5 (RLT-abcde, BLD-abcde, DEV-abcde)

Here is the code: 这是代码:

use warnings;
use strict;

my @file_list = `find /folder -type f -name '*.ext'|xargs cleartool lsvtree -all`;

foreach my $file(@file_list){

  if ($file=~m/(.+)(main)(.\d)/g){ 
   print $file;
  }
}

I'm pretty sure that I'm making a stupid mistake somewhere , but I just can't see it! 我很确定自己在某个地方犯了一个愚蠢的错误,但是我看不到它!

Thank you in advance for your advice. 预先感谢您的建议。

PS I tried it under Perl 5.8 an Perl 5.18 with the same results, OS is Solaris. PS我在Perl 5.8和Perl 5.18下尝试过,结果相同,操作系统是Solaris。

Change 更改

print $file;

to: 至:

print "$MATCH\n";

so you only print the part of the line that was matched by the regexp. 因此,您只打印与正则表达式匹配的行的一部分。

You should also change \\d to \\d+ , to allow for numbers with more than one digit. 您还应该将\\d更改为\\d+ ,以允许数字多于一个数字。

Just after a quick look 快速浏览之后

/foldera/folderb/folderc/folderd/file.ext@@/main /foldera/folderb/folderc/folderd/file.ext@@/main

Has no number at the end. 末尾没有数字。 And \\d requires the number ;-) 和\\ d需要数字;-)

You may also find this site usefull: http://regexpal.com/ 您可能还会发现此站点有用: http : //regexpal.com/

I think is not matching your line because your regex is explicitly looking for a digit at the end 我认为与您的行不匹配,因为您的正则表达式在末尾明确寻找一个数字

Try changing your regex to be: (Note the curley brackets at the end) 尝试将正则表达式更改为:(注意最后的居里括号)

(.+)(main)(.\\d){0,1} (。+)(main)(。\\ d){0,1}

Or personally I would write it like this: 或者我个人会这样写:

(.*?)main(\\/\\d*){0,1} (。*?)main(\\ / \\ d *){0,1}

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM