[英]Substitute the markdown italic to html using regex in Perl
To convert the markdown italic text $script
into html, I've written this:要将 markdown 斜体文本$script
转换为 html,我这样写:
my $script = "*so what*";
my $res =~ s/\*(.)\*/$1/g;
print "<em>$1</em>\n";
The expected result is:预期结果是:
<em>so what</em>
but it gives:但它给出:
<em></em>
How to make it give the expected result?如何让它给出预期的结果?
Problems:问题:
.
won't match more than one character.不会匹配超过一个字符。Fix:使固定:
$script =~ s{\*([^*]+)\*}{<em>$1</em>}g;
print "$script\n";
or或者
my $res = $script =~ s{\*([^*]+)\*}{<em>$1</em>}gr;
print "$res\n";
But that's not it.但事实并非如此。 Even with all the aforementioned problems fixed, your parser still has numerous other bugs.即使解决了上述所有问题,您的解析器仍然存在许多其他错误。 For example, it misapplies italics for all of the following:例如,它错误地将斜体应用于以下所有内容:
**Important**
4 * 5 * 6 = 120
4 * 6 = 20 is *wrong*
`foo *bar* baz`
foo *bar* baz
正确: foo *bar* baz
\*I like stars\*
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.