Re: shrinking the path
by bart (Canon) on Feb 01, 2005 at 09:10 UTC
|
A few remarks:
- quotemeta, using "\Q" inside the regex (see also perlop), to search for literal substrings instead of a pattern
- Do not put extra quotes in your substitution pattern, it'll cause it to fail
- There's no need to match and substitute in two steps. s/// will just return a true flag if it indeed did substitute anything, and do nothing otherwise
- Anchor the match to the beginning of the string, using ^, or you'll allow it to substitute anywhere in the string — unless that's what you want?
The resulting code is:
if($folder_path =~ s/^\Q$sourcedir/Eitv9/) {
print "\n$folder_path/$file";}
Oh and if the path comes from the user using STDIN, don't forget to chomp it first.
| [reply] [d/l] |
|
Thanks bart !
it worked , even before posting i tried what boris had suggested but that lead to a failure why ?
prad
| [reply] |
|
my $string = 'c:\\Windows';
my $search = '\\W';
my $qm = quotemeta($search);
my $raw = $string; $raw=~ s/$search/#/;
my $cooked = $string; $cooked =~ s/\Q$search/#/;
print <<"TEST"
original string: '$string'
search string: '$search'
search string after quotemeta: '$qm'
substitution without \\Q: '$raw'
substitution with \\Q: '$cooked'
TEST
Result:
original string: 'c:\Windows'
search string: '\W'
search string after quotemeta: '\\W'
substitution without \Q: 'c#\Windows'
substitution with \Q: 'c:#indows'
As you can see, with quotemeta, it replaced the substring backslash+"W", because that's what /\\W/ searches for. Without it, it just searched for the first non-word character and replaced it — that happens to be a ":". | [reply] [d/l] [select] |
Re: shrinking the path
by perlsen (Chaplain) on Feb 01, 2005 at 10:22 UTC
|
$folder_path =~ s#^\Q$sourcedir\E#Eitv9#i
| [reply] [d/l] |
Re: shrinking the path
by holli (Abbot) on Feb 01, 2005 at 09:57 UTC
|
use strict;
use warnings;
#sourcedir, as it comes from <STDIN>, hence it has a newline
my $sourcedir = 'C:\Documents and Settings\Administrator\Desktop\eit
+v9_lvttest' . "\n";
my $folder_path = 'C:\Documents and Settings\Administrator\Desktop\eit
+v9_lvttest';
my $file = "test";
#get rid of newline
chomp $sourcedir;
#escape backslashes and other possible regex-affecting meta-characters
my $sourcedir_regex = quotemeta($sourcedir);
if( $folder_path =~ s/$sourcedir_regex/Eitv9/ )
{
print "\n$folder_path/$file";
}
This prints "Eitv9/test".
| [reply] [d/l] |
Re: shrinking the path
by radiantmatrix (Parson) on Feb 01, 2005 at 14:32 UTC
|
$source_path = 'C:\Documents and Settings\Administrator\Desktop\';
## Note the single quotes
$folder_path = 'C:\Documents and Settings\Administrator\Desktop\eitv9
+_lvttest';
## Replace: remember to escape your '\', and to replace case-insensiti
+vely.
$folder_path =~ s[^$source_path][Eitv9\\]i;
radiantmatrix
require General::Disclaimer;
s//2fde04abe76c036c9074586c1/; while(m/(.)/g){print substr(' ,JPacehklnorstu',hex($1),1)}
| [reply] [d/l] |
Re: shrinking the path
by borisz (Canon) on Feb 01, 2005 at 09:10 UTC
|
$folder_path=~s/$sourcedir/Eitv9/;
Note that this line replace your path even if it is somewhere in the middle of $folder_path.
| [reply] [d/l] |
|
that won´t work, because of the backslashes in $sourcestring. They must be escaped.
| [reply] |
Re: shrinking the path
by jkva (Chaplain) on Feb 01, 2005 at 09:11 UTC
|
I am trying to understand what you want to do but I fail to. Could you be a bit more clearer in what you are trying to achieve?
Good luck,
Detonite | [reply] |
Re: shrinking the path
by Tanktalus (Canon) on Feb 01, 2005 at 16:33 UTC
|
To be honest, I stopped using regexp's for this a while ago. I use substr now:
if ($folder_path =~ /^$sourcedir/) {
$folder_path = File::Spec->catdir('Eitv9',substr($folder_path, lengt
+h $source_dir));
}
That said, if you want to use regexp's for this, you don't need the "if":
$folder_path =~ s/^\Q$sourcedir/Eitv9/;
That's because "if" it doesn't match, the substitution will fail (silently - no error message), and $folder_path will be left alone. | [reply] [d/l] [select] |
Re: shrinking the path
by archen (Pilgrim) on Feb 02, 2005 at 00:36 UTC
|
I'd be inclined to do this (assuming that file and the directory are together):
use File::Basename;
print 'Evit9/' . basename( $folder_path );
If I understand your intentions. Think it's a bit more readable then a regex. | [reply] [d/l] |