Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Strings in Regex??

Status
Not open for further replies.

NavMen

Vendor
Jul 6, 2004
35
NL
Hi,
How can I remove a part of a string, what I try to do is removing the ($dir1) C:\eval form the string $filename example c:\eval\new document.doc.

And when there is a sub folder I like to display \Folder\New document.txt. without the c:\eval

The following script gives me already a lot of info, but I don’t get the Regex correct $filTest=~s/$remove//; I don’t know how I must use Strings in this context?

Can someone give me a possible solution..

Thnx,
Navmen

#!/usr/local/bin/perl -w
use strict;
use Errno;
use Getopt::Std;
#use Win32::TieRegistry ( Delimiter=>"/", ArrayValues=>1 );
use Time::Local;

use file::compare;
use Win32::ODBC;

use strict;
my $dir1 ='c:\eval';
my $remove = 'c:\eval';
do_dir($dir1);
exit;

sub do_dir {
my $dir = shift;
opendir(D, $dir);
my @f = readdir(D);
closedir(D);
foreach my $file (@f) {
my $filename = $dir . '\\' . $file;
if ($file eq "." || $file eq "..") {
} elsif (-d $filename) {
# depending on your needs you can do subdirs
do_dir($filename);
} else {
# do something with $filename, like ...
print "$dir1 \n";
print "FileName: $filename \n";
my $filTest = $filename;
$filTest=~s/$remove//;
print "NoDrive: $filTest \n";
#print "$file\n";
}
}
}
 
Code:
#!perl
$filename = 'c:\eval\subfolder\document.txt';
print "$filename\n";
$filename =~ s/(c:\\)eval\\(.*$)/$1$2/i;
print "$filename\n";

'hope this helps

If you are new to Tek-Tips, please use descriptive titles, check the FAQs, and beware the evil typo.
 
Hi,

change the line
Code:
my $remove = 'c:\eval';
TO
Code:
my $remove = quotemeta 'c:\eval';
ALSO, use qr operator will have faster result
Code:
$remove = qr/$remove/;
Good luck! ;-)
 
Thanks,

The my $dir1 ='c:\eval'; and my $remove = 'c:\eval'; are now fixed info.

What must be done when $dir1 and $remove are coming from a SQL database!

I tested the $remove = /qr/$remove/;
and have the following result:

Start Dir: c:\eval
FileName: c:\eval\New Folder\New Microsoft Word Document.doc
NoDrive: (?-xism:c:\eval)
Start Dir: c:\eval
FileName: c:\eval\New Text Document.txt
NoDrive: (?-xism:c:\eval)
 
Hi,

if $dir1 and $remove aren't fixed, you still can use the "quotemeta"
Code:
$remove = quotemeta $remove;
$remove = qr/$remove/

About the qr//, I tried this in my computer.
Code:
my $dir1 ='c:\eval';
my $remove = quotemeta 'c:\eval';
$remove = qr/$remove/;

do_dir($dir1);

sub do_dir
{
    my $dir = shift;
    
    opendir(D, $dir);
    my @f = readdir(D);
    closedir(D);
    
    foreach my $file (@f)
    {
        my $filename = $dir . '\\' . $file;

        if ($file eq "." || $file eq "..")
	{
        }
	elsif (-d $filename)
	{
            # depending on your needs you can do subdirs
            do_dir($filename);
        }
	else
	{
	    
            # do something with $filename, like ...

	    print " \$filename = $filename\n";
	    print " \$remove   = $remove\n";

	    if ($filename =~ s/$remove//)
	    {
		print " NoDrive   = \"$filename\"\n";
	    }
        }
    }
}

And get this result.
Code:
 $filename = c:\eval\New Bitmap Image.bmp
 $remove   = (?-xism:c\:\\eval)
 NoDrive   = "\New Bitmap Image.bmp"

[ponder]
 
Hi eewah,

I have to learn reading :) It works as you say!

Many thanks,

NavMen
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top