-2

To convert the markdown italic text $script into html, I've written this:

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?

ikegami
  • 367,544
  • 15
  • 269
  • 518
Terry
  • 1,206
  • 1
  • 10
  • 26
  • There are some modules at CPAN already. Just google `perl cpan markdown`. No need to roll yet another. – Kosh Feb 11 '22 at 18:37
  • I don't think CPAN module should be used for such a simple problem. – Terry Feb 11 '22 at 18:47
  • 2
    Terry-- This is not so simple at all. How are you going to handle cases that @ikegami's answer brings up? And that is not an exhaustive list by any means. – zdim Feb 11 '22 at 18:54
  • @zdim Well the target text I 'm processing doesn't contain many of those cases. This doesn't need to be a one-size-fits-for-all solution anyway but rather a quick fix for me who hasn't used regex for years :) – Terry Feb 11 '22 at 19:01
  • If you had been using `use warnings` you would have been warned about your problem with the warning `Use of uninitialized value $1 in string... ` It might have alerted you to the fact that your regex didn't match, and in fact you might have figured out that `(.)` needs a quantifier like `+` to match that string. Always use `use strict; use warnings`. – TLP Feb 11 '22 at 22:17

1 Answers1

3

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.
  • You always add one EM element, even if multiple pairs of stars are found.
  • You add the EM element around the entire output, not just the portion in stars.

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
    Your code: 4 5 6 = 120
  • 4 * 6 = 20 is *wrong*
    Correct: 4 * 6 = 20 is wrong
    Your code: 4 6 = 20 is wrong*
  • `foo *bar* baz`
    Correct: foo *bar* baz
    Your code: `foo bar baz`
  • \*I like stars\*
    Correct: *I like stars*
    Your code: \I like stars\
ikegami
  • 367,544
  • 15
  • 269
  • 518
  • Thank you for the answer. It worked instantly. I've edited the question, it was missing the `$script` – Terry Feb 11 '22 at 18:57
  • 2
    1) That's just one of the *many* things wrong. (See update). 2) Don't (attempt to) edit the question to remove the problem. If you want to post a solution, feel free to answer your own question using an Answer. I have reverted the fix. 3) I had missed that problem. I have updated my answer to address it. – ikegami Feb 11 '22 at 20:21