use strict;
my $str = 'This text is not italic.
*This text is italic.*
This text is *partially* italic
This text has *two* *italic* bits
**bold text (not italic)**
**bold text with *italic* **
**part bold,** *part italic*
*italic text **with bold** *
*italic* **bold** *italic* **bold**
*invalid markdown (do not parse)**
random * asterisk';
my $regex = qr/[^[:graph:]*](?#Do not match on any visible character)
\K\*(?#Reset starting point and then match on a *)
([[:alpha:].,\ ]+(?:\*{2}[[:alpha:].,\ ]+\*{2}?\ ?)?)(*COMMIT)
\*\ ?(?#Match on ending * then stop matching with COMMIT)/xmp;
my $subst = '<i>$1</i> ';
my $result = $str =~ s/$regex/$subst/rg;
print "The result of the substitution is' $result\n";
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Perl, please visit: http://perldoc.perl.org/perlre.html