in reply to Checking number in file name

Would I be correct in inferring from your code that the "account number" is always at the start of the filename and followed by an underscore? In that case, a simple index test would do (and I think should be pretty efficient). Something like this:

#!/usr/bin/perl use strict; use warnings; my $filename = "000231263444_01_XY_20130110_061717.txt"; my $accountnumber = '000231263444'; if (index($filename, "${accountnumber}_") == 0) { print "Found\n"; } else { print "Not found\n"; }

Replies are listed 'Best First'.
Re^2: Checking number in file name
by Anonymous Monk on May 19, 2013 at 15:46 UTC
    Yes, it will be at that start of the file name but no underscore will be part of the file name:
    my $filename = "000231263444_01_XY_20130110_061717.txt"; #after getting it with reg exp. my $accountnumber = '000231263444';

      Splitting is probably better for you than using a regex. Especially if you later want to do something with the other parts of the filename, too.

      my ( $accountnumber, $part2, $part3, $part4, $part5, $extension ) = sp +lit /[_.]/, $filename;