in reply to Matching condition for two files

Try something like

use strict; use warnings; while(<DATA>){ if($_ =~ /M8BBABONP\wM\d+/){ print "Match found\n"; } } __DATA__ M8BBABONPGM100.dat M8BBABONPYM101.dat

Output:
Match found
Match found

Replies are listed 'Best First'.
Re^2: Matching condition for two files
by CountZero (Bishop) on May 02, 2011 at 09:57 UTC
    Your regex will also match filenames with more than one digit different.

    Also it will match filenames which will begin with anything else and which have different extensions. To avoid that you will need to anchor the regex:

    /^M8BBABONP\wM10\d\.dat$/

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

      CountZero++. I had tried it and got stuck in some muck, but I used your regex and a foreach:
      #!/usr/bin/perl use strict; use warnings; use Data::Dumper::Concise; my(@array) = ('M8BBABONPGM100.dat', 'M8BBABONPYM101.dat'); foreach my $array(@array) { if ($array =~ /^M8BBABONP\wM10\d\.dat$/){ print "We have a match: \n"; print Dumper($array); } }
Re^2: Matching condition for two files
by AnomalousMonk (Archbishop) on May 02, 2011 at 12:52 UTC

    Assuming the differing letter and digit will always be in the same positions...

    The character set  \w also matches digits and _ (underscore). A more specific character set is  [[:alpha:]] (the set of all alphabetic characters), or  [[:upper:]] if you know the letter will always be upper-case. Also, heed the advice of CountZero concerning anchoring the ends of the match and file name extensions.

    See Character Classes and other Special Escapes in perlre, and Posix Character Classes in perlrecharclass.

Re^2: Matching condition for two files
by Anonymous Monk on May 02, 2011 at 07:36 UTC
    why no + after \w
    if($_ =~ /M8BBABONP\w+M\d+/){

      because + used for one or more matching and in this situation we know that only one letter is changing after that letter "M" will come. so for one letter \w is enough.

        ok.thanks