Re: Removing dots
by tlm (Prior) on May 08, 2005 at 14:06 UTC
|
It's hard to say what the best solution would be without knowing exactly what dots you don't want removed, but for the example you give,
s/\.//;
does the job. Or
s/\.(?!txt$)//g
...or
s/\.(?![^.]+$)//g
| [reply] [Watch: Dir/Any] [d/l] [select] |
|
ok let's say if i want it to look like Hello.1txt would s/\.(?=txt$)//g do the job?
| [reply] [Watch: Dir/Any] |
|
% perl -de 1
DB<1> $s = 'Hello.1.txt'
DB<2> $s =~ s/\.(?=txt$)//g # in this case the /g is superfluous
DB<3> p $s
Hello.1txt
And faster still (in the long run) if you just study perlre.
| [reply] [Watch: Dir/Any] [d/l] |
|
$filename =~ /\./;
substr($filename, $-[0]+1) =~ s/\.//g if @-;
| [reply] [Watch: Dir/Any] [d/l] |
Re: Removing dots
by Dietz (Curate) on May 08, 2005 at 14:11 UTC
|
This would remove all dots except the rightmost one:
s/\.(?=.*\.)//g;
A single dot occuring only once in the string will not be removed.
| [reply] [Watch: Dir/Any] [d/l] |
Re: Removing dots
by davidrw (Prior) on May 08, 2005 at 14:47 UTC
|
If it's just the base name you need to manipulate (i.e. leave the '.txt. intact), can take advantage of File::Basename to parse that section for you, then do what you want with it. That way the regex is simple because it doesn't have to deal w/the exception of the extension.
# given: $fullname, @suffixlist (see pod for latter)
use File::Basename;
my ($name,$path,$suffix) = fileparse($fullname,@suffixlist);
$name =~ s/\.//g;
my $new_fullname = $path . $name . $suffix;
| [reply] [Watch: Dir/Any] [d/l] |
Re: Removing dots
by ambrus (Abbot) on May 08, 2005 at 16:09 UTC
|
| [reply] [Watch: Dir/Any] |
Re: Removing dots
by Angharad (Pilgrim) on May 08, 2005 at 14:53 UTC
|
How about using a combination of substring and then cat the resulting variables together? | [reply] [Watch: Dir/Any] |
|
| [reply] [Watch: Dir/Any] |
Re: Removing dots
by mattk (Pilgrim) on May 08, 2005 at 23:15 UTC
|
Just because I felt like doing something different than an RE:
$origfile = shift;
@chunks = split(/\./, $origfile);
$ext = pop @chunks;
$filename = join("", @chunks, ".$ext");
rename $origfile, $filename;
| [reply] [Watch: Dir/Any] [d/l] |