I was thinking about ways to do HTML shortcuts for my blog, and I came up with this idea -- any block of text enclosed by [ and ] on a line by themselves automagically becomes an ordered list.

So I come up with this:

$string = ' blah blah blah before-list text [ line one line 1 line one line 1 line 2 line two line 2 line two line line number 3 line three line number 3 line three line 4 ] blah blah later after-list text '; $string =~ s! ^\[$ # open-bracket as entire line (.*) # all including linebreaks - /s modifier ^\]$ # close-bracket as entire line !$x=$1; $x =~ s/^/<li>/g; # LIs at the start of each line "<ol>$x</ol>"; # return whole thing !smegx; # smeg -- how cool is that? print $string;
which I was quite proud of -- but instead of what I thought I'd get:
blah blah blah before-list text <ol> <li>line one line 1 line one line 1 <li>line 2 line two line 2 line two line <li>line number 3 line three line number 3 line three <li>line 4 </ol> blah blah later after-list text
I got this:
blah blah blah before-list text <ol><li> <li>line one line 1 line one line 1 <li>line 2 line two line 2 line two line <li>line number 3 line three line number 3 line three <li>line 4 </ol> blah blah later after-list text
and I can't quite figure out where that extra <li> has come from. The next '^' isn't right after the last '$'?

I'm sure my fellow monks can tell me where I'm going wrong.



“Every bit of code is either naturally related to the problem at hand, or else it's an accidental side effect of the fact that you happened to solve the problem using a digital computer.”
M-J D

In reply to automagic-HTML regex by Cody Pendant

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.