in reply to Regular Expression Assistance

One more thing I should specify: I know what the directory should be and I know the alphabetic part of the filename...the only part I don't know is the number part. So, in the example I gave, I know that the pattern I want to find has /home/monks/thanksforhelping and I know that it ends in .ext, but I don't know that it contains 1234567890...I only know that it contains some random string of numbers.

Replies are listed 'Best First'.
Re: Re: Regular Expression Assistance
by marcos (Scribe) on Jun 10, 2002 at 14:50 UTC
    I would do it using File::Basename to separate the path from the filename. This simplifies the task a little: you just have to extract the number from the filename:
    #!/usr/bin/perl -w use strict; use File::Basename; while (<>) { my ($name, $path, $suffix) = fileparse ($_, ".txt"); $name =~ /\D+(\d+)/; print "number: $1\n"; }

    I hope this helps.

    marcos
Re: Re: Regular Expression Assistance
by janx (Monk) on Jun 10, 2002 at 13:23 UTC
    If you know the absolute filename and want to open that file, what prevents you from doing just that?

    _Ass_uming safe input data here:

    my $filename = $ARGV[0] or die "No filename given\n"; open INPUT, "<", $filename or die "could not open $filename\n"; while (<INPUT>) { print $_ }; close INPUT;
    Anyway:
    If the complete filename is in $filename you can do:
    my ($file_without_path) = $filename =~ /^.*?\/(\w*?\w*?\.ext)$/;

    and end up with something like: "file23432545335.ext"

    Still, I don't see the point if you trust the filename as being safe, and just want to open it. (Since you will need the path anyway).

    janx