1

I need to highlight an email addresses in text but not highlight them if contained in HTML tags, content, or attributes.

For example, the string example@example.com must be converted to <a href="mailto:example@example.com">example@example.com</a>

But email addresses in the string <a href="mailto:example@example.com">example@example.com</a> must not be processed.

I've tried something like this regexp:

(?<![":])[a-zA-Z0-9._%-+]+@[a-zA-Z0-9._%-]+.[a-zA-Z]{2,6}(?!")

but it doesn't work properly.

msw
  • 42,753
  • 9
  • 87
  • 112
SvartalF
  • 163
  • 2
  • 8
  • duplicate: http://stackoverflow.com/questions/401726/regex-that-only-matches-text-thats-not-part-of-html-markup-python – msw Apr 29 '10 at 02:30

1 Answers1

1

I'll guessing that your source text is an HTML file which is missing anchor tags for only some of the contained e-mail addresses. If this is true, then you will not be able to use a regexp to reliably match only untagged e-mail addresses. For example, given the input:

...
<P>You'll find a lot more written by <A 
href="mailto:SvartaIF@example.com"
title="some text including an@sign.org">

SvartaIF@example.com
</A>.
</P>
...

it becomes impossible to lexically associate the href with the address and also exclude an@sign.org. You need to use an HTML parser; BeautifulSoup is pretty popular.

msw
  • 42,753
  • 9
  • 87
  • 112