in reply to match *bold* formatting, but avoid html
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/<([^>]*<)/\<$1/g; # Replace unmatched < with < 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 < <u>$20</u>, you <b>need</b> <a href="...">this</a>
(Update - simplified code, egregious abuse of $_)
|
|---|