Wednesday, September 16, 2009

.Net Regular Expression Tutorial - Part 1 (Basics)


What is Regular Expression?

It is a way of representing data using symbols. They are often used within matching,
searching or replacing algorithms.The term regular expression is often abbreviated
to regex or RE, RegEx or RegExp.

In this series of tutorials i will start from basic operations or Regular Expression
to more complex one with .Net C# code example. Hopefully you will find it very easy
to learn.

String Replacement with Case Sensitive and Insensitive Options

I am mentioning this basic replace functionality of Regular Expression class. Though
we have a replace functionality with String class. But problem with the lateral
is that it is case sensitive and if you need to do replacement with case insensitive you need to do a workaround. RegEx class provide us with a static method with parameter for Case ignoring. Following code shows difference between case sensitive options.
                             
string text = "This is DotNetFocus blog. DotNetfocus blog is resource for ASP.NET.";
string pattern = "DotNetFocus"; //Simple string to match. keeping things simple in the start.
string replacewith = "MyBlog";

string updated = Regex.Replace(text, pattern, replacewith, RegexOptions.None);
Console.WriteLine(updated); //Output-> This is MyBlog blog. DotNetfocus blog is resource for ASP.NET.

updated = Regex.Replace(text, pattern, replacewith, RegexOptions.IgnoreCase);
Console.WriteLine(updated); //Output-> This is MyBlog blog. MyBlog blog is resource for ASP.NET.


Your feedback is an encourgement to write more.

Tuesday, August 18, 2009

Remove carriage return line feed in T-SQL (SQL Server)

Remove carriage return, line feed and tab in T-SQL..

REPLACE(REPLACE(REPLACE(MyField, CHAR(10), ''), CHAR(13), ''), CHAR(9), '')


Hope this will help
..

Saturday, August 1, 2009

System.Web.UI.WebControls.TextBox Maxlength doest not work when TextMode is set to Multiline

Hi,

This is known issue in ASP.NET Standard Text box control when set its TextMode="MultiLine" the MaxLength property does not work.

There is a workaround to accomplish this using RegularExpressionValidator. Following is the markup that will restrict the text box to maximum of 500 characters.


<asp:TextBox ID="txtComments" runat="server" TextMode="MultiLine" Height="100px"
Width="320px" MaxLength="10"></asp:TextBox> <asp:RegularExpressionValidator
ID="regComments" runat="server" ControlToValidate="txtComments"
ValidationExpression="^[\s\S]{0,500}$" ErrorMessage="Maximum 500 characters are allowed in comments box." Text="Maximum 500 characters are allowed in comments
box." > </asp:RegularExpressionValidator>



Hope this will help,
Khurram