in reply to Perl Regex \A or ^ Not Working

As for hippo, your first case of "^audit" works for me. Could you show the contents of @dir_files using Data::Dumper?

In the second case, note that "\Aaudit" produces the warning "Unrecognized escape \A passed through" (you have warnings turned on, right?). This is because in a double-quoted string, \A doesn't have a meaning. In general, this can be fixed by using qr// to precompile your regexes, as in my $pattern = qr/\Aaudit/;.

However, since you say this is user input, if the user is inputting \Aaudit, then your string will actually be "\\Aaudit" in Perl syntax, which should work, but if it's not, you need to show us code that is more representative of what you're doing for us to be able to see what's wrong, otherwise we're just guessing.

For example, are you chomping your user's input? Use Data::Dumper with $Data::Dumper::Useqq=1; to look at $pattern (and show us that, too).

Replies are listed 'Best First'.
Re^2: Perl Regex \A or ^ Not Working
by thundercat (Novice) on Mar 29, 2019 at 12:25 UTC
    No I did not have warnings on.
    my $pattern = '\\Aaudit'; my $no_file_error ="TRUE"; my @dir_files = $ftp->dir($ftp_directory); $Data::Dumper::Useqq=1; print Dumper($pattern); my $netrc = Net::Netrc->lookup($ftp_server) or die ("Could not find lo +gin info in .netrc for $ftp_server"); my $ftp = Net::FTP->new($ftp_server, Port => $ftp_server_port, De +bug => 1) or die ('Could not create ftp object'); $ftp->login($netrc->login, $netrc->password) or die ('Could not l +og in'), $ftp->message; $ftp->binary(); #print directory of files before command print "--------------------First List all files------------------ +\n"; my @dir_files = $ftp->dir($ftp_directory); foreach my $list(@dir_files){ print "$list\n"; } @dir_files = grep { $_ =~ /$pattern/ } @dir_files; if (@dir_files == 0) { if ($no_file_error eq "TRUE") { print "No files exist with that $pattern\n"; } else { print "Send Email: Cannot find files to GET for parameter $pat +tern"; }} else { for my $list(@dir_files){ print "$ftp_command $list\n"; } }
    I added data::dumper and warnings Output with audit
    $VAR1 = "audit"; Net::FTP>>> Net::FTP(2.77) Net::FTP>>> Exporter(5.63) Net::FTP>>> Net::Cmd(2.29) Net::FTP>>> IO::Socket::INET(1.31) Net::FTP>>> IO::Socket(1.31) Net::FTP>>> IO::Handle(1.28) Net::FTP=GLOB(0xaec9e8)<<< 220 Titan FTP Server 11.20.2264 Ready. Net::FTP=GLOB(0xaec9e8)>>> USER xupromgr Net::FTP=GLOB(0xaec9e8)<<< 331 User name okay, need password. Net::FTP=GLOB(0xaec9e8)>>> PASS .... Net::FTP=GLOB(0xaec9e8)<<< 230-Welcome xupromgr from 10.253.2.119. You + are now logged in to the server. Net::FTP=GLOB(0xaec9e8)<<< 230 User logged in, proceed. Net::FTP=GLOB(0xaec9e8)>>> TYPE I Net::FTP=GLOB(0xaec9e8)<<< 200 Type set to I. --------------------First List all files------------------ Net::FTP=GLOB(0xaec9e8)>>> PASV Net::FTP=GLOB(0xaec9e8)<<< 227 Entering Passive Mode (10,253,1,56,236, +13). Net::FTP=GLOB(0xaec9e8)>>> LIST /AS/testftp Net::FTP=GLOB(0xaec9e8)<<< 150 File status okay; about to open data co +nnection. Net::FTP=GLOB(0xaec9e8)<<< 226 Closing data connection. Transferred 41 +5 bytes. drw-rw---- 1 xupromgr xupromgr 512 Mar 29 08:04 . drw-rw---- 1 xupromgr xupromgr 512 Mar 28 13:54 .. -rw-rw---- 1 xupromgr xupromgr 18 Feb 27 14:21 no_audit.txt -rw-rw---- 1 xupromgr xupromgr 18 Feb 27 14:21 no_audit_date +.txt -rw-rw---- 1 xupromgr xupromgr 10 Feb 27 14:08 auditsample.t +xt -rw-rw---- 1 xupromgr xupromgr 11 Feb 27 14:21 audit_date.tx +t get -rw-rw---- 1 xupromgr xupromgr 18 Feb 27 14:21 no_audit. +txt get -rw-rw---- 1 xupromgr xupromgr 18 Feb 27 14:21 no_audit_ +date.txt get -rw-rw---- 1 xupromgr xupromgr 10 Feb 27 14:08 auditsamp +le.txt get -rw-rw---- 1 xupromgr xupromgr 11 Feb 27 14:21 audit_dat +e.txt
    This list is what is outputted with ^audit
    $VAR1 = "^audit"; Net::FTP>>> Net::FTP(2.77) Net::FTP>>> Exporter(5.63) Net::FTP>>> Net::Cmd(2.29) Net::FTP>>> IO::Socket::INET(1.31) Net::FTP>>> IO::Socket(1.31) Net::FTP>>> IO::Handle(1.28) Net::FTP=GLOB(0x1cdf9e8)<<< 220 Titan FTP Server 11.20.2264 Ready. Net::FTP=GLOB(0x1cdf9e8)>>> USER xupromgr Net::FTP=GLOB(0x1cdf9e8)<<< 331 User name okay, need password. Net::FTP=GLOB(0x1cdf9e8)>>> PASS .... Net::FTP=GLOB(0x1cdf9e8)<<< 230-Welcome xupromgr from 10.253.2.119. Yo +u are now logged in to the server. Net::FTP=GLOB(0x1cdf9e8)<<< 230 User logged in, proceed. Net::FTP=GLOB(0x1cdf9e8)>>> TYPE I Net::FTP=GLOB(0x1cdf9e8)<<< 200 Type set to I. --------------------First List all files------------------ Net::FTP=GLOB(0x1cdf9e8)>>> PASV Net::FTP=GLOB(0x1cdf9e8)<<< 227 Entering Passive Mode (10,253,1,56,235 +,244). Net::FTP=GLOB(0x1cdf9e8)>>> LIST /AS/testftp Net::FTP=GLOB(0x1cdf9e8)<<< 150 File status okay; about to open data c +onnection. Net::FTP=GLOB(0x1cdf9e8)<<< 226 Closing data connection. Transferred 4 +15 bytes. drw-rw---- 1 xupromgr xupromgr 512 Mar 29 08:04 . drw-rw---- 1 xupromgr xupromgr 512 Mar 28 13:54 .. -rw-rw---- 1 xupromgr xupromgr 18 Feb 27 14:21 no_audit.txt -rw-rw---- 1 xupromgr xupromgr 18 Feb 27 14:21 no_audit_date +.txt -rw-rw---- 1 xupromgr xupromgr 10 Feb 27 14:08 auditsample.t +xt -rw-rw---- 1 xupromgr xupromgr 11 Feb 27 14:21 audit_date.tx +t No files exist with that ^audit
    This is with the variable set to \\Aaudit
    $VAR1 = "\\Aaudit"; Net::FTP>>> Net::FTP(2.77) Net::FTP>>> Exporter(5.63) Net::FTP>>> Net::Cmd(2.29) Net::FTP>>> IO::Socket::INET(1.31) Net::FTP>>> IO::Socket(1.31) Net::FTP>>> IO::Handle(1.28) Net::FTP=GLOB(0x18539e8)<<< 220 Titan FTP Server 11.20.2264 Ready. Net::FTP=GLOB(0x18539e8)>>> USER xupromgr Net::FTP=GLOB(0x18539e8)<<< 331 User name okay, need password. Net::FTP=GLOB(0x18539e8)>>> PASS .... Net::FTP=GLOB(0x18539e8)<<< 230-Welcome xupromgr from 10.253.2.119. Yo +u are now logged in to the server. Net::FTP=GLOB(0x18539e8)<<< 230 User logged in, proceed. Net::FTP=GLOB(0x18539e8)>>> TYPE I Net::FTP=GLOB(0x18539e8)<<< 200 Type set to I. --------------------First List all files------------------ Net::FTP=GLOB(0x18539e8)>>> PASV Net::FTP=GLOB(0x18539e8)<<< 227 Entering Passive Mode (10,253,1,56,236 +,32). Net::FTP=GLOB(0x18539e8)>>> LIST /AS/testftp Net::FTP=GLOB(0x18539e8)<<< 150 File status okay; about to open data c +onnection. Net::FTP=GLOB(0x18539e8)<<< 226 Closing data connection. Transferred 4 +15 bytes. drw-rw---- 1 xupromgr xupromgr 512 Mar 29 08:04 . drw-rw---- 1 xupromgr xupromgr 512 Mar 28 13:54 .. -rw-rw---- 1 xupromgr xupromgr 18 Feb 27 14:21 no_audit.txt -rw-rw---- 1 xupromgr xupromgr 18 Feb 27 14:21 no_audit_date +.txt -rw-rw---- 1 xupromgr xupromgr 10 Feb 27 14:08 auditsample.t +xt -rw-rw---- 1 xupromgr xupromgr 11 Feb 27 14:21 audit_date.tx +t No files exist with that \Aaudit

      If you had used Data::Dumper to look at @dir_files like I suggested, you'd see strings such as "-rw-rw---- 1 xupromgr xupromgr           10 Feb 27 14:08 auditsample.txt", which won't match the regex /^audit/. Try using the command $ftp->ls instead of $ftp->dir to list files, this should give you a listing consisting of file names only.

      You should always Use strict and warnings, and see also the Basic debugging checklist.

        Thanks, changing to ls did it

        Thanks changing to ls worked.