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

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.

Replies are listed 'Best First'.
Re^6: regex find and replace with a twist
by haukex (Archbishop) on Jul 17, 2018 at 13:21 UTC
    join ( '', map { "[$_]" } split /[a-zA-Z]/, $word );

    Sorry, I think that's also not right:

    $ perl -wMstrict -MData::Dump -e \ 'dd join "", map { "[$_]" } split /[a-zA-Z]/, q($-.%abc&/)'; "[\$-.%][][][&/]"

    Whereas, by my understanding of the question (that does still need to be clarified), the output should be '$-.%[a][b][c]&/'.

      Good point -- I was only answering the follow-up, not the reply. And now I've spent an entertaining hour tinkering with Perl regular expressions, time which is never wasted. :)

      Here's my improved code, which shows that I still have some distance to go in writing regular expressions:

      tab@music3:~/2018-0717$ cat !$ cat pm2.pl #!/usr/bin/perl use strict; use warnings; { my $word = q($-.%abc&/); my @group1 = ( $word =~ m/(?: # Non-capturing group of captured ([^a-zA-Z]+) # .. non-matching ([a-zA-Z]+) # .. following by matching )+ # .. for as many groups as possible ([^a-zA-Z]+) # followed by matching. /x ); my @group2 = map { $_ =~ /[a-zA-Z]/ ? split( //, $_ ) : $_ } @group1; my $new_word = join ( '', map { "[$_]" } @group2 ); print "$word -> $new_word\n"; } tab@music3:~/2018-0717$ perl pm2.pl $-.%abc&/ -> [$-.%][a][b][c][&/] tab@music3:~/2018-0717$

      Alex / talexb / Toronto

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

        I don't understand the effort to use split and map in solutions. Even with this latest version of the assumed implied requirement, a good, old  s/// seems the best way:

        c:\@Work\Perl\monks>perl -wMstrict -le "my $word = '$-.%aBc&/d-E'; ;; (my $new_word = $word) =~ s{ ([^[:alpha:]]+ | [[:alpha:]]) }{[$1]}xms +g; ;; print qq{'$word' -> '$new_word'}; " '$-.%aBc&/d-E' -> '[$-.%][a][B][c][&/][d][-][E]'


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

        BTW: The method used here fails in the following cases:

        c:\@Work\Perl\monks>perl -wMstrict -le "for my $word (qw(XYZ$-.%abc&/ $-.%abc&/XYZ $-.%abc&/XYZ+=*)) { my @group1 = ( $word =~ m/(?: ([^a-zA-Z]+) ([a-zA-Z]+) )+ ([^a-zA-Z]+) /x ); my @group2 = map { $_ =~ /[a-zA-Z]/ ? split( //, $_ ) : $_ } @group1; my $new_word = join ( '', map { qq{[$_]} } @group2 ); print qq{'$word' -> '$new_word'}; } " 'XYZ$-.%abc&/' -> '[$-.%][a][b][c][&/]' '$-.%abc&/XYZ' -> '[$-.%][a][b][c][&/]' '$-.%abc&/XYZ+=*' -> '[&/][X][Y][Z][+=*]'
        (Again, this assumes that we can, to begin with, even arrive at a common understanding of what is required. :)


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