Gossamer Forum
Home : General : Perl Programming :

Getting rid of leading .'s?

Quote Reply
Getting rid of leading .'s?
Hi. I'm trying to figure out some regex to do the following;

1) Remove any non-standard charachters, and replace them with _ (i.e @!"$%^&*()- etc)
2) If the file is named file.image.gif, I want to replace the first . with a _. For example;

name.image.gif, would become name_image.gif.

Not all files age going to be like this.. for example;

file.gif
image.jpg

...etc.

Any ideas? i would imagine something like this to get rid of non-words;

$var =~ s/\W/_/gi;

...but I'm confused about the dot part :(

TIA

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Getting rid of leading .'s? In reply to
I'd say you're looking for the ^ regex flag that indicates the beginning of the string.

Maybe something like this will get you started.

Code:
if ($_ =~ /^\.(.?)/) {
$_ =~ s/^\.(.?)/$1/;
}

Edit: That . should probably be escaped with a \.

- wil

Last edited by:

Wil: Oct 8, 2003, 7:42 AM
Quote Reply
Re: [Wil] Getting rid of leading .'s? In reply to
Thanks, that seems to work like a charm :)

Code:
$img = GT::CGI::escape($img);
if ($img =~ /^\.(.?)/) { $img =~ s/^\.(.?)/$1/; }
$img =~ s/\W//gi;

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!