Quoth Bill H <>:
> On Jun 20, 1:48*pm, Ben Morrow <b...@morrow.me.uk> wrote:
> > Quoth Bill H <b...@ts1000.us>:
> >
> > > Can someone point me to some docs on how I would do this without
> > > iterating over the whole string (pattern matching?):
> >
> > > $original = "a malformed%string/containi\"ng characters I don'~t
> > > want! ...";
> >
> > > $filter = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-_";
> >
> > > $new = &fix($original);
> >
> > > $new would now equal:
> >
> > > amalformedstringcontainingcharactersidontwant
> >
> > $orginal =~ tr/a-zA-Z0-9_-//cd;
> >
> > See tr/// under 'Regexp Quote-Like Operators' in perlop.
> >
> > Note that '-' must come first or last, as otherwise it will be
> > interpreted as part of an X-Y range.
>
> Ben this one seems to work nice for me, only issue is I have to lc()
> the results (not really a problem). Is there a way to have that done
> in this ?
Well, tr/-_a-zA-Z0-9\000-\177/-_a-za-z0-9/d will work, but personally
I'd probably just use lc, and probably beforehand, to avoid two
alphabetic ranges. Something like
(my $new = lc $original) =~ tr/a-z0-9_-//cd;
seems clearer than relying on the details of tr///, which is an operator
a lot of Perl programmers don't know very well.
Ben
--
Outside of a dog, a book is a man's best friend.
Inside of a dog, it's too dark to read.
Groucho Marx