in reply to Re: regex find and replace with a twist
in thread regex find and replace with a twist

what if $word = '$-.%'?

Replies are listed 'Best First'.
Re^3: regex find and replace with a twist
by talexb (Chancellor) on Jul 17, 2018 at 12:41 UTC

    That's simple to try .. I suspect you didn't try it.

    tab@music3:~/2018-0717$ cat pm.pl #!/usr/bin/perl use strict; use warnings; { my $word = '$-.%'; my $new_word = join ( '', map { "[$_]" } split '', $word ); print "$word -> $new_word\n"; } tab@music3:~/2018-0717$ perl pm.pl $-.% -> [$][-][.][%] tab@music3:~/2018-0717$

    Perl doesn't care what's in the string -- more specifically, split doesn't have problems with any characters that may have special meaning in a regex.

    Alex / talexb / Toronto

    Thanks PJ. We owe you so much. Groklaw -- RIP -- 2003 to 2013.

      $-.% -> [$][-][.][%]

      Note the OP said (emphasis mine):

      I need to find any letter [a-zA-Z] and replace it

        Ugh .. I hate mis-reading the specs.

        tab@music3:~/2018-0717$ cat pm.pl #!/usr/bin/perl use strict; use warnings; { my $word = '$-.%'; my $new_word = join ( '', map { "[$_]" } split /[a-zA-Z]/, $word ) +; print "$word -> $new_word\n"; } tab@music3:~/2018-0717$ perl pm.pl $-.% -> [$-.%] tab@music3:~/2018-0717$

        In addition, I should have used // in my original call to split, as it takes a regex and not a string. Thanks for the reminder.

        Alex / talexb / Toronto

        Thanks PJ. We owe you so much. Groklaw -- RIP -- 2003 to 2013.