简体   繁体   中英

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

/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.

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.

Just after a quick look

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

Has no number at the end. And \\d requires the number ;-)

You may also find this site usefull: 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}

Or personally I would write it like this:

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

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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