RE: regular expression in a variable



That appears to work! The part I am stuck on is how to I take that value
(which would now be $file in your example) and put it into a variable that I
can use through the rest of the script. When I then try to use $file outside
of that routine, it is no longer the same value.

I really appreciate your help!



-----Original Message-----
From: Timothy Johnson [mailto:tjohnson@xxxxxxxxxxxx]
Sent: Tuesday, February 28, 2006 3:47 PM
To: Curt Shaffer; beginners@xxxxxxxx
Subject: RE: regular expression in a variable


So what part are you stuck on then? It looks like the first suggestion
gets you the $filename you want. All you have to do after that is move
it.

(you can change it so it isn't looking in the current directory with the
opendir line, but if you do, don't forget that you can't move the files
by filename alone, you must add the path as a prefix)

-----Original Message-----
From: Curt Shaffer [mailto:cshaffer@xxxxxxxxx]
Sent: Tuesday, February 28, 2006 12:42 PM
To: Timothy Johnson; beginners@xxxxxxxx
Subject: RE: regular expression in a variable

Thanks for the response. Let me try to clear things up. The second
solution
will not work for me because the other parts of the filename will be
unknown. The only part I know will always be in the filename is "test"
(only
an example).

So the full story is this:

I need to look in a directory to see if a file with "test" in the name
exists. If it does I need to move that file to a staging directory to be
sftp'd out to a vendor. The manner in which I am doing that is to move
the
file named $filename (whose value is the result of the regex match) to
the
staging directory. Then sftp the $filename to the appropriate place.

Does that help a bit? If not I apologize.

Curt

-----Original Message-----
From: Timothy Johnson [mailto:tjohnson@xxxxxxxxxxxx]
Sent: Tuesday, February 28, 2006 3:30 PM
To: Curt Shaffer; beginners@xxxxxxxx
Subject: RE: regular expression in a variable


You're not being very clear what it is you're trying to do. I can see
two ways of interpreting this.

Regular expressions are mostly for checking the format of text to see if
certain conditions "match". You might be asking how to do this:

########################

use strict;
use warnings;
opendir(DIR,".") or die("Couldn't open the current directory!\n");
my @files = readdir(DIR);
foreach my $file(sort @files){
if($file =~ /(.*test.*)/i){
print "MATCH: $file\n";
}
}

#########################

You can also use the $1 variable to capture the last text string that
matched the part of the regular expression between the parentheses.


If, on the other hand, you're trying to generate file names, then
regular expressions aren't what you're looking for.

my $prefix = "123";
my $postfix = "456";
my $filename = $prefix."test".$postfix;
print $filename."\n";




-----Original Message-----
From: Curt Shaffer [mailto:cshaffer@xxxxxxxxx]
Sent: Tuesday, February 28, 2006 12:20 PM
To: beginners@xxxxxxxx
Subject: regular expression in a variable

I need to set a variable to a filename where only 1 section of the file
is
static.

For example:

$filename =~ /test/;

Where the following:

Print "$filename\n";

Would produce:

123test456.txt



.



Relevant Pages

  • RE: regular expression in a variable
    ... If you want to hold on to those fiels which match your criteria, then use another array to capture as in ... regular expression in a variable ... suggestion gets you the $filename you want. ... If it does I need to move that file to a staging directory to ...
    (perl.beginners)
  • RE: regular expression in a variable
    ... regular expression in a variable ... suggestion gets you the $filename you want. ... If it does I need to move that file to a staging directory to ... You can also use the $1 variable to capture the last text string that ...
    (perl.beginners)
  • RE: regular expression in a variable
    ... gets you the $filename you want. ... regular expression in a variable ... If it does I need to move that file to a staging directory to be ... file named $filename (whose value is the result of the regex match) to ...
    (perl.beginners)
  • RE: regular expression in a variable
    ... The only part I know will always be in the filename is "test" (only ... If it does I need to move that file to a staging directory to be ... file named $filename (whose value is the result of the regex match) to the ... matched the part of the regular expression between the parentheses. ...
    (perl.beginners)
  • Re: Regular expression for check that an entered filename is valid
    ... Im looking for a regular expression to check that a entered filename ... I have a cms Im building where the user can enter the name ...
    (comp.lang.php)