Automatically hyper-link URL's and Email's using C#, whilst leaving bespoke tags in place

Posted by marcusstarnes on Stack Overflow See other posts from Stack Overflow or by marcusstarnes
Published on 2010-06-14T13:36:34Z Indexed on 2010/06/14 15:42 UTC
Read the original article Hit count: 291

Filed under:
|
|

I have a site that enables users to post messages to a forum.

At present, if a user types a web address or email address and posts it, it's treated the same as any other piece of text.

There are tools that enable the user to supply hyper-linked web and email addresses (via some bespoke tags/markup) - these are sometimes used, but not always. In addition, a bespoke 'Image' tag can also be used to reference images that are hosted on the web.

My objective is to both cater for those that use these existing tools to generate hyper-linked addresses, but to also cater for those that simply type a web or email address in, and to then automatically convert this to a hyper-linked address for them (as soon as they submit their post).

I've found one or two regular expressions that convert a plain string web or email address, however, I obviously don't want to perform any manipulation on addresses that are already being handled via the sites bespoke tagging, and that's where I'm stuck - how to EXCLUDE any web or email addresses that are already catered for via the bespoke tagging - I wan't to leave them as is.

Here are some examples of bespoke tagging for the variations that I need to be left alone:

[URL=www.msn.com]www.msn.com[/URL]

[URL=http://www.msn.com]http://www.msn.com[/URL]

[[email protected]][email protected][/EMAIL]

[IMG]www.msn.com/images/test.jpg[/IMG]

[IMG]http://www.msn.com/images/test.jpg[/IMG]

The following examples would however ideally need to be automatically converted into web & email links respectively:

www.msn.com

http://www.msn.com

[email protected]

Ideally, the 'converted' links would just have the appropriate bespoke tags applied to them as per the initial examples earlier in this post, so rather than:

<a href="..." etc.

they'd become:

[URL=http://www.. etc.)

Unfortunately, we have a LOT of historic data stored with this bespoke tagging throughout, so for now, we'd like to retain that rather than implementing an entirely new way of storing our users posts.

Any help would be much appreciated.

Thanks.

© Stack Overflow or respective owner

Related posts about c#

Related posts about ASP.NET