My approach does split out the lines into markup/non-markup, which you said you want to avoid. I use a simple hash to remember whether it was time for an open or close tag, or no tag at all to prevent opening a tag and not closing it.

It handles nested tags (e.g., */stuff/*), but doesn't check for mis-nested tags like Tye's solution does.

Enjoy.

#!/usr/bin/perl -w use strict; my %sc = qw ( \\* b / em _ u ); my %counts; my %opened; sub track { $opened{$_} = 1 - ( $opened{$_} || 0 ); return "<$sc{$_}>" if --$counts{$_} > 0 && $opened{$_}; return "</$sc{$_}>" if ! $opened{$_}; $_; } sub reg_fix { my $line=shift; %counts = (); %opened = (); $line =~ s/<([^>]*<)/\&lt;$1/g; # Replace unmatched < with &lt; my @elems = split '(<.*?>)', $line; $line =~ s/<.*?>//g; $counts{$_} = @{[$line =~ m/$_/g]} for keys %sc; for my $elem ( grep !/^</, @elems ) { $elem =~ s#$_#&track#eg for + keys %sc; } join '',@elems; } print reg_fix($_) for <DATA> __DATA__ go/to <img src='http://allpoetry.com:8080/images/smile/happy.gif'> /th +x/ <img src='http://allpoetry.com:8080/images/smile/happy.gif'> _sill +y *to_* Well, if < _$20_, you *need* <a href="...">this</a>

Which produces :

go<em>to <img src='http://allpoetry.com:8080/images/smile/happy.gif'> +</em>thx/ <img src='http://allpoetry.com:8080/images/smile/happy.gif' +> <u>silly <b>to</u></b> Well, if &lt; <u>$20</u>, you <b>need</b> <a href="...">this</a>

(Update - simplified code, egregious abuse of $_)


In reply to Re: match *bold* formatting, but avoid html by delirium
in thread match *bold* formatting, but avoid html by Anonymous Monk

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.