简体   繁体   English

在 Perl 中使用正则表达式将 markdown 斜体替换为 html

[英]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:问题:

  • You print the wrong variable.您打印了错误的变量。
  • You switch variable names halfway through.您在中途切换变量名称。
  • . won't match more than one character.不会匹配超过一个字符。
  • You always add one EM element, even if no stars are found.你总是添加一个 EM 元素,即使没有找到星星。
  • You always add one EM element, even if multiple pairs of stars are found.你总是添加一个 EM 元素,即使发现了多对恒星。
  • You add the EM element around the entire output, not just the portion in stars.您在整个 output 周围添加 EM 元素,而不仅仅是星星中的部分。

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**
    Correct: Important正确:重要
    Your code: * Important *您的代码:*重要*
  • 4 * 5 * 6 = 120
    Correct: 4 * 5 * 6 = 120正确:4 * 5 * 6 = 120
    Your code: 4 5 6 = 120您的代码:4 5 6 = 120
  • 4 * 6 = 20 is *wrong*
    Correct: 4 * 6 = 20 is wrong正确:4 * 6 = 20 是错误
    Your code: 4 6 = 20 is wrong*您的代码:4 6 = 20 是错误的*
  • `foo *bar* baz`
    Correct: foo *bar* baz正确: foo *bar* baz
    Your code: `foo bar baz`您的代码:`foo bar baz`
  • \*I like stars\*
    Correct: *I like stars*正确:*我喜欢星星*
    Your code: \ I like stars\你的代码:\我喜欢星星\

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM