#!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11112988 use warnings; my $wiki = '_/one *two*/ th/ree_ null _/four *five*/ six_ null _/se_ven *eight*/ nine_'; my $expected = 'one two th/ree null four five six null se_ven eight nine'; my %h = ( '*' => 'b' , '/' => 'i' , '_' => 'u' , ); my $html = $wiki =~ s{ (?:^|\s) \K ([*_/]+) | ([*_/]*) (?=$|\s) } { $1 ? $1 =~ s|.|<$h{$&}>|gr : $2 =~ s|.||gr }gexr; print $html eq $expected ? "passed" : "FAILED", "\n\n"; print $wiki, "\n\n", $expected, "\n\n", $html, "\n";