Regular expression – strings in the single click using Perl

My opinion:

my $tmp = "rrccllrrc";

Expected output :

$tmp = "right right center center left left right right center"; #End should not be spaced definitely.

My code:< /p>

$tmp=~s/c/center /g;
$tmp=~s/l/left /g;
$tmp=~s /r/right /g;

Can someone help shorten the way to replace as many regular expressions as possible.

There can also be no regular expressions

my %repl = (c =>'center', l =>'left' , r =>'right');

$tmp = join '', map {$repl{$_}} split'', $tmp;

With pattern’ The split splits the string into a list of characters, and map uses a hash to replace them all with whole words. The output list of the map is connected by spaces.

Update to comment

If the original If the string contains other characters, you can filter it out first

$tmp = join '', map {$repl{$_}} grep {/c|l| r/) split'', $tmp;

Or, use any content in the empty list in the map that is not defined in the hash

 $tmp = join '', map {$repl{$_} // ()} split'', $tmp;

This completely deletes everything except c | l | r. Let They stay in the result

$tmp = join '', map {$repl{$_} // $_} split' ', $tmp;

They are also separated by spaces. To keep them together, you need to further adjust it.

My opinion:

my $tmp = "rrccllrrc";

Expected output:

 $tmp = "right right center center left left right right center"; #End should not be spaced definitely.

My code:

$tmp =~s/c/center /g;
$tmp=~s/l/left /g;
$tmp=~s/r/right /g;

Someone It can help shorten the way to replace as many regular expressions as possible.

There can also be no regular expressions

p>

my %repl = (c =>'center', l =>'left', r =>'right');

$tmp = join '', map {$ repl{$_}} split'', $tmp;

The split with pattern ‘breaks down the string into a list of characters, and map uses hashing to replace them all with whole words. The output list of the map Connected by spaces.

Update to comment

If the original string contains other characters, you can filter it out first

 $tmp = join '', map {$repl{$_}} grep {/c|l|r/} split'', $tmp;

Or, use the empty list in the map Anything not defined in the hash

$tmp = join '', map {$repl{$_} // ()} split'', $tmp; 

This completely deletes everything except c|l|r. Keep them in the result

$tmp = join '', map {$repl{$_} // $_} split'', $tmp;

They are also separated by spaces. Keep them together , It needs to be adjusted further.

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 5426 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.