Showing posts with label regex. Show all posts
Showing posts with label regex. Show all posts

TEXT TO HTML REGEX METHOD



//TEXT TO HTML REGEX METHOD
public string TextToHtml(string inputtext )
{
inputtext = Regex.Replace(inputtext, "<", "<") ;
inputtext = Regex.Replace(inputtext, ">", ">") ;
inputtext = Regex.Replace(inputtext, @"http://(.\S+)", "$1") ;
inputtext = Regex.Replace(inputtext, @"ftp://(.\S+)", "$1ftp://$1\">$1;") ;
inputtext = Regex.Replace(inputtext, @"news://(.\S+)", "$1;") ;
inputtext = Regex.Replace(inputtext, @"(\S+\@.*\..\S+)", "$1;") ;
inputtext = Regex.Replace(inputtext, "\n", "
") ;
inputtext = Regex.Replace(inputtext, @"\s\s", " ") ;
return inputtext ;
}




REGEX expression for HTML Tag cleaning

C# code for replacing either a specific tag, or removing a series of potentially malicious tags from an HTML string:

 
using System.Text.RegularExpressions

// Replace BODY tag with "<div id="advert/">" (for later replacement with ad code, for example):
strContent = Regex.Replace(strContent, @"</?(?I:BODY)(.|\N)*?>", "<div id="advert/">");


//Malicious script: replace any and all of script / body /embed / object / frameset / frame / iframe / meta / ling /style with "")
strContent = Regex.Replace(strContent, @"</?(?I:SCRIPT|BODY|EMBED|OBJECT|FRAMESET|FRAME|IFRAME|META|LINK|STYLE)(.|\N)*?>", "");

Regular Expression to convert URL to Hyperlink


using System.Text.RegularExpressions;
//this will turn all urls in the input parameter string sInput into
HTML hyperlinks

public string ConvertURLsToHyperlinks(string sInput)
{
return Regex.Replace(sInput, @"(\bhttp://[^ ]+\b)", @"$0");
}