9/16/2012

Demo Code

 public static bool IsValidURL(string strURL)
{
    string regExPattern = @"^^(ht|f)tp(s?)\:\/\/[0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*(:(0-9)*)*(\/?)([a-zA-Z0-9\-\.\?\,\'\/\\\+&%\$#_=]*)?$";
    Regex re = new Regex(regExPattern);
    return re.IsMatch(strURL);
}
demo highlight foo bar

 

Demo Quote

Save And Share :

8/25/2012

Easy Change the Text Selection Color In Blogger

It very simple to change your select sentence or a single word prevent by default with set as blue color. Now let's start:

1.  Go to Blogger Dashboard > Template > Edit HTML.
2.  Back up your template first if you don't want make some problem :)
3.  Check the Expand Widget Templates checkbox.
4.  Now search (CTRL+F) for the code tag below:
 ]]></b:skin>
And now add the below code before it
::-moz-selection{background: #9E0ADC;color: white;}
::selection{background: #9E0ADC;color: white;}
You can see demo on my blog. Hope you enjoyed doing this trick for your blog. 

Save And Share :

6/11/2012

Regex quick reference

[abc] A single character of: a, b or c
[^abc] Any single character except: a, b, or c
[a-z] Any single character in the range a-z
[a-zA-Z] Any single character in the range a-z or A-Z
^ Start of line
$ End of line
\A Start of string
\z End of string
. Any single character
\s Any whitespace character
\S Any non-whitespace character
\d Any digit
\D Any non-digit
\w Any word character (letter, number, underscore)
\W Any non-word character
\b Any word boundary character
(...) Capture everything enclosed
(a|b) a or b
a? Zero or one of a
a* Zero or more of a
a+ One or more of a
a{3} Exactly 3 of a
a{3,} 3 or more of a
a{3,6} Between 3 and 6 of a

options: i case insensitive m make dot match newlines x ignore whitespace in regex o perform #{...} substitutions only once

Save And Share :

Remove Html Tag in C#

You want to remove HTML tags from your string. This is useful for displaying HTML in plain text and stripping formatting like bold and italics, while not removing any actual textual content. Test the methods available for this functionality for performance and correctness with test cases.

These C# example programs show how to remove HTML tags from strings.

Removing HTML tags from strings

Input:    <p>The <b>dog</b> is <i>cute</i>.</p>
Output:   The dog is cute.

Performance test for HTML removal

HtmlRemoval.StripTagsRegex:         2404 ms
HtmlRemoval.StripTagsRegexCompiled: 1366 ms
HtmlRemoval.StripTagsCharArray:      287 ms [fastest]

File length test for HTML removal

File tested:                        Real-world HTML file
File length before:                 8085 chars
HtmlRemoval.StripTagsRegex:         4382 chars
HtmlRemoval.StripTagsRegexCompiled: 4382 chars
HtmlRemoval.StripTagsCharArray:     4382 chars

Examples

First, here is a static class that tests three different ways of removing HTML tags and their contents. The methods receive string arguments and then process the string and return new strings that do not have the HTML tags. The methods have different performance characteristics. As a reminder, HTML tags start with < and end with >.

HtmlRemoval static class [C#]

using System;
using System.Text.RegularExpressions;

/// <summary>
/// Methods to remove HTML from strings.
/// </summary>
public static class HtmlRemoval
{
    /// <summary>
    /// Remove HTML from string with Regex.
    /// </summary>
    public static string StripTagsRegex(string source)
    {
	return Regex.Replace(source, "<.*?>", string.Empty);
    }

    /// <summary>
    /// Compiled regular expression for performance.
    /// </summary>
    static Regex _htmlRegex = new Regex("<.*?>", RegexOptions.Compiled);

    /// <summary>
    /// Remove HTML from string with compiled Regex.
    /// </summary>
    public static string StripTagsRegexCompiled(string source)
    {
	return _htmlRegex.Replace(source, string.Empty);
    }

    /// <summary>
    /// Remove HTML tags from string using char array.
    /// </summary>
    public static string StripTagsCharArray(string source)
    {
	char[] array = new char[source.Length];
	int arrayIndex = 0;
	bool inside = false;

	for (int i = 0; i < source.Length; i++)
	{
	    char let = source[i];
	    if (let == '<')
	    {
		inside = true;
		continue;
	    }
	    if (let == '>')
	    {
		inside = false;
		continue;
	    }
	    if (!inside)
	    {
		array[arrayIndex] = let;
		arrayIndex++;
	    }
	}
	return new string(array, 0, arrayIndex);
    }
}

Notes. This is a public static class written in the C# language that does not save state. You can call into the class using the code HtmlRemoval.StripTags*. Normally, you can put this class in a separate file named HtmlRemoval.cs. Because it is not project-specific, it is useful for many programs.

StripTagsRegex. This method uses a static call to Regex.Replace, and therefore the expression is not compiled. For this reason, this method could be optimized by pulling the Regex out of the method, such as in the second method. The regular expression specifies that all sequences matching < and > with any number of characters, but the minimal number, are replaced with string.Empty (removed).

StripTagsRegexCompiled. This method does the exact same thing as the previous method, but its regular expression is pulled out of the method call and stored in the static class. I recommend this method for most programs, as it is very simple to inspect and considerably faster than the first method. The static Regex will only be created once in your program.

StripTagsCharArray. This method is a heavily optimized version of an approach that could instead use StringBuilder. In most benchmarks, this method is faster and is appropriate for when you need to strip lots of HTML files. A detailed description of the method's body is available below.

Tests

Here we look at a program that runs these methods through a very simple test. The three methods work identically on valid HTML. One thing you should note is that the char array method will strip anything that follows a <, but the Regex methods will require a > before they strip the tag.

Program that tests HTML removal [C#]

using System;
using System.Text.RegularExpressions;

class Program
{
    static void Main()
    {
	const string html = "<p>There was a <b>.NET</b> programmer " +
	    "and he stripped the <i>HTML</i> tags.</p>";

	Console.WriteLine(HtmlRemoval.StripTagsRegex(html));
	Console.WriteLine(HtmlRemoval.StripTagsRegexCompiled(html));
	Console.WriteLine(HtmlRemoval.StripTagsCharArray(html));
    }
}

Output

There was a .NET programmer and he stripped the HTML tags.
There was a .NET programmer and he stripped the HTML tags.
There was a .NET programmer and he stripped the HTML tags.

 

First, from my performance research I know regular expressions in the C# language are usually not the fastest way to process test. I wrote an algorithm that uses a combination of char arrays and the new string constructor to strip HTML tags, filling the requirement and often performing better.

The benchmark for these methods stripped 10000 HTML files of around 8000 characters in tight loops. The file was read in from File.ReadAllText. The result was that the char array method was considerably faster. This could worthwhile to use if you have to strip many files in a script, such as one that preprocesses a large website in memory.

Iterative method

The method here that uses char arrays and is dramatically faster than the other two methods uses a neat algorithm for parsing the HTML quickly. It iterates through all characters, flipping a flag Boolean depending on whether it is inside a tag block. It only adds characters to the array buffer if it is not a tag. For performance, it uses char arrays and the new string constructor that accepts a char array and a range. This is faster than using StringBuilder.

Using RegexOptions.Compiled and a separate Regex results in better performance than using the Regex static method. RegexOptions.Compiled has some drawbacks, however. It can reduce startup time by 10x in some cases. More material is available pertaining to make Regexes simpler and faster to run.

Self-closing tags

In XHTML, certain elements such as BR and IMG have no separate closing tag, and instead use the "/>" at the end of the first tag. The test file noted includes these self-closing tags, and the methods handle it correctly. Here are some HTML tags supported.

Supported tags

<img src="" />
<img src=""/>
<br />
<br/>
< div >
<!-- -->

Source: dotnetperls.com

See some example:

How to remove Html Tag form Client site with jQuery

Save And Share :

Remove Html Tag with jQuery

The example below show how to remove HTML tags from strings with jQuery

 

INPUT:         <b>We</b>love<span>jQuery.</span>

OUTPUT:   We love jQuery.

jQuery source:

var NewString = OriginalString.replace(/(<([^>]+)>)/ig, "");

Thumbs up

Save And Share :

 
  • Followers