in reply to regex find and replace with a twist
my $word = 'layer'; my $new_word = join '', map { "[$_]" } split '', $word; # Or, if I want to be more explicit about the process: # my @raw_chars = split '', $word; # my @cooked_chars = map { "[$_]" } @raw_chars; # my $new_word = join '', @cooked_chars;
Edit: As pointed out in replies, the spec was to only bracket characters in the range a-zA-Z, not all characters. This can be fixed with a minor adjustment to the map. Change it to map { $_ =~ /[a-zA-Z]/ ? "[$_]" : $_ } and you get the result:
$ perl -E '$word = "layer123 $-.% foo"; $new_word = join "", map { $_ +=~ /[a-zA-Z]/ ? "[$_]" : $_ } split "", $word; say $new_word;' [l][a][y][e][r]123 0.% [f][o][o]
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: regex find and replace with a twist
by AnomalousMonk (Archbishop) on Jul 17, 2018 at 15:29 UTC | |
|
Re^2: regex find and replace with a twist
by Anonymous Monk on Jul 17, 2018 at 07:36 UTC | |
by talexb (Chancellor) on Jul 17, 2018 at 12:41 UTC | |
by haukex (Archbishop) on Jul 17, 2018 at 13:01 UTC | |
by talexb (Chancellor) on Jul 17, 2018 at 13:11 UTC | |
by haukex (Archbishop) on Jul 17, 2018 at 13:21 UTC | |
|