Home > Archive > PERL Beginners > March 2006 > substitute all but the last
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
substitute all but the last
|
|
| S.A. Birl 2006-03-22, 6:58 pm |
| Im doing some file renaming.
Im looking to subtitute all but the last . into _
ie: filename.jpg.pgp --> filename_jpg.pgp
ie: filename.2.jpg.pgp --> filename_2_jpg.pgp
ie: file....._.jpg.pgp --> file__________.pgp
Ive tried about 10 different expressions, but they've all failed.
The closest one I have is
s/\.[^[\..+$]]/_/;
But I cant wrap my mind around the correct expression. Help.
Thanks
Birl
Please do not CC me responses to my own posts.
I'll read the responses on the list.
| |
| John W. Krahn 2006-03-22, 6:58 pm |
| S.A. Birl wrote:
> Im doing some file renaming.
>
> Im looking to subtitute all but the last . into _
> ie: filename.jpg.pgp --> filename_jpg.pgp
> ie: filename.2.jpg.pgp --> filename_2_jpg.pgp
> ie: file....._.jpg.pgp --> file__________.pgp
>
>
> Ive tried about 10 different expressions, but they've all failed.
> The closest one I have is
> s/\.[^[\..+$]]/_/;
>
> But I cant wrap my mind around the correct expression. Help.
$ perl -e'
my @files = qw[ filename.jpg.pgp filename.2.jpg.pgp file....._.jpg.pgp ];
for my $file ( @files ) {
print "$file --> ";
$file =~ s/\.(?=.*\.)/_/g;
print "$file\n";
}
'
filename.jpg.pgp --> filename_jpg.pgp
filename.2.jpg.pgp --> filename_2_jpg.pgp
file....._.jpg.pgp --> file_______jpg.pgp
John
--
use Perl;
program
fulfillment
| |
| Tom Phoenix 2006-03-22, 6:58 pm |
| On 3/22/06, S.A. Birl <sbirl+PERL@concept.temple.edu> wrote:
> Im looking to subtitute all but the last . into _
> ie: filename.jpg.pgp --> filename_jpg.pgp
> ie: filename.2.jpg.pgp --> filename_2_jpg.pgp
> ie: file....._.jpg.pgp --> file__________.pgp
There are many ways to do this; here's the first one I thought of,
using a lookahead assertion to see that there's another dot remaining:
s/\.(?=3D.*\.)/_/gs
Hope this helps!
--Tom Phoenix
Stonehenge Perl Training
| |
| S.A. Birl 2006-03-22, 6:58 pm |
| On Mar 22, John W. Krahn (nospam-krahnj@telus.net.ns) typed:
> $ perl -e'
> my @files = qw[ filename.jpg.pgp filename.2.jpg.pgp file....._.jpg.pgp ];
> for my $file ( @files ) {
> print "$file --> ";
> $file =~ s/\.(?=.*\.)/_/g;
> print "$file\n";
> }
> '
> filename.jpg.pgp --> filename_jpg.pgp
> filename.2.jpg.pgp --> filename_2_jpg.pgp
> file....._.jpg.pgp --> file_______jpg.pgp
On Mar 22, Tom Phoenix (nospam-tom@stonehenge.com.ns) typed:
> There are many ways to do this; here's the first one I thought of,
> using a lookahead assertion to see that there's another dot remaining:
>
> s/\.(?=.*\.)/_/gs
Thanks a lot Tom and John! Works. :)
Thanks
Birl
Please do not CC me responses to my own posts.
I'll read the responses on the list.
| |
| Tom Arnall 2006-03-22, 6:58 pm |
| Tom, John,
could you explain in detail what the stuff in parens is about, i.e.,
'?=.*\.' ?
thanks,
tom arnall
north spit, ca
On Wednesday 22 March 2006 12:41 pm, Tom Phoenix wrote:
> On 3/22/06, S.A. Birl <sbirl+PERL@concept.temple.edu> wrote:
>
> There are many ways to do this; here's the first one I thought of,
> using a lookahead assertion to see that there's another dot remaining:
>
> s/\.(?=.*\.)/_/gs
>
> Hope this helps!
>
> --Tom Phoenix
> Stonehenge Perl Training
| |
| Tom Phoenix 2006-03-22, 6:58 pm |
| On 3/22/06, tom arnall <kloro@cox.net> wrote:
> could you explain in detail what the stuff in parens is about, i.e.,
> '?=3D.*\.' ?
When the first thing inside the parentheses is a question mark, it's a
sign that the parentheses are doing something special.
s/\.(?=3D.*\.)/_/gs
The perlre manpage explains that (?=3D...) is "a zero-width positive
look-ahead assertion." It matches if the pattern inside (that is,
between the "=3D" and the closing parenthesis) would match at the
current match position, but it doesn't change the match position. That
is, it "looks ahead" to see whether something would match, but the
lookahead part isn't involved in the substitution.
So, the pattern match is matching (and thus replacing) only a dot, so
long as it's eventually followed by another dot.
Does that give you what you needed? Cheers!
--Tom Phoenix
Stonehenge Perl Training
|
|
|
|
|