in reply to Re: Is there a way to make these two regex lines cleaner?
in thread Is there a way to make these two regex lines cleaner?

So, if you choose substitution: s/[^a-zA-Z0-9"' .?!-]//g

if transliteration (would be my choice): y/a-zA-Z0-9"' .?!-//cd

I'd still be inclined to escape the '-' in both cases: someone is all too likely to come along in a couple of years needing to add one more character to the allowed list, and the natural tendency would be to add it to the end.

Replies are listed 'Best First'.
Re^3: Is there a way to make these two regex lines cleaner?
by AnomalousMonk (Archbishop) on Apr 17, 2022 at 22:55 UTC

    A hyphen placed at the beginning of a character class or tr/// search/replace list is also interpreted literally:

    Win8 Strawberry 5.8.9.5 (32) Sun 04/17/2022 18:43:19 C:\@Work\Perl\monks >perl use strict; use warnings; my $s = '123-abc-456'; $s =~ tr/-a-z//cd; print "'$s' \n"; $s = '123-xyz-456'; $s =~ s/[^-a-z]//g; print "'$s' \n"; ^Z '-abc-' '-xyz-'
    But one can argue that one is as likely to place new stuff at the start as at the end, so escaping remains wise. :)


    Give a man a fish:  <%-{-{-{-<

Re^3: Is there a way to make these two regex lines cleaner?
by kcott (Archbishop) on Apr 18, 2022 at 04:40 UTC

    I've been putting '-' at the end for a very long time (probably decades) and have never encountered the scenario you describe; however, I'm not averse to a bit of defensive programming. :-)

    Update: The remainder of what I originally wrote is just wrong: I'll put it down to an idiotic brain fart. I've stricken it and, because it was quite long, removed it to a spoiler.

    — Ken