Another approach:

> I basically want to replace the string &,&#,&#3,&#38 to blank, but it should not replace &

This meets your "requirements" and is IMHO easier to understand and more intuitive than tybald89's solution

DB<19> p $test &,&#,&#3,&#38,&#38;&,&#,&#3,&#38,&#38; DB<20> p $test =~ s/(&#\d+;)|&#?\d*/$1/gr ,,,,&#38;,,,,&#38;

The trick is to first match correct entities and leave them unchanged by replacing them with themselves.

Incorrect entities are then found by backtracking and replaced with an empty $1.

Please handle this with care, I'm not sure if your requirements didn't miss edge cases.

NB: yes, it will also replace &38 without #

DB<22> p ',&38,' =~ s/(&#\d+;)|&#?\d*/$1/gr ,,

otherwise you can add other or-conditions to exclude this case.

Cheers Rolf
(addicted to the Perl Programming Language and ☆☆☆☆ :)
Je suis Charlie!

update

replaced s/(&#?\d*;)|... with s/(&#\d+;)|


In reply to Re: Need a regex to replace incomplete html entities by LanX
in thread Need a regex to replace incomplete html entities by Chris Daniel

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.