You might want to try for something more like:

m!<a(\s+[\w]+\s*\=\s*('[^']*'|"[^"]*"))*\s*>.*?</a\s*>!i

Update: at Abigail-II's suggestion, here's a modified version of the above, which accepts tags like <a href= foo_link>link text</a>. Of course, comments and criticism are always welcome.

m!<a(\s+[\w]+\s*\=\s*('[^']*'|"[^"]*"|[a-z0-9\-\._:]+))*\s*>.*?</a\s*> +!i

This should be what you're looking for, because (if I got it right) it successfully detects any valid anchor tag. Once you've got that, you can substitute stuff for SGML entities wherever you haven't found a valid tag, like s/"/&quot;/g and so forth. That way, any invalid code gets printed verbatim. Instead of:
A link with no closing tag where there really should be one...
You'll see
A <a href="#">link with no closing tag where there really should be one...

One limitation might pop up if the users start nesting anchors inside one another... This is why my initial response if it were my own app and server would be "get smarter users" :o)

Anyway, as always, there's bound to be faults with what I wrote above. Here's what I used to test it:

#!/usr/bin/perl for (<>) { m!<a(\s+[\w]+\s*\=\s*('[^']*'|"[^"]*"))*\s*>.*?</a\s*>!i ? pri +nt "match: " : print "no match: "; print; }
And my dataset:
<a href="foo">blah</a> <a href="foo>blah</a> <a href='foo" >blah</a> <a href="foo">blah</b> <a href="foo's">bar</a> <a name="blah" href="foo" >bar</a>
And my results:
match: <a href="foo">blah</a> no match: <a href="foo>blah</a> no match: <a href='foo" >blah</a> no match: <a href="foo">blah</b> match: <a href="foo's">bar</a> match: <a name="blah" href="foo" >bar</a>

LAI
:eof

In reply to Re^2: RegEx for incorrectly closed HTML attribute? by LAI
in thread RegEx for incorrectly closed HTML attribute? 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.