SIGN UP MEMBER LOGIN:    
ARTICLE

Magic of Regex using C#.Net

Posted by Sushmita Kumari Articles | How do I March 31, 2006
This article is all about Regex which shows different functionalities of the RegularExpression Class with sample code.
Reader Level:
Download Files:
 

Regular expression is a compact way of describing complex patterns in texts. You can use them to search for patterns and, once found, to modify the patterns in complex ways. They can also be used to launch programmatic actions that depend on patterns.

 

Regular Expressions offers an extremely powerful way to search, replace, format and extract text data inside a string using sophisticated search criteria. For example, you can search for whole words, or find a match with some pattern (e-mails, phone numbers, postal codes, HTML or XML tags, etc.), and can quickly locate, count, and extract all the occurrences from the searched string.

 

The Regex class also contains several static methods that allow you to use regular expressions without instantiating a Regex object. This reduces the amount of code you have to write, and is appropriate if the same regular expression is used only once or reused seldomly. Note that member overloading is used a lot in the Regex class. All the static methods have the same names (but different parameter lists) as other non-static methods.

Most formalisms provide the following operations to construct regular expressions.

 

Alternation

A vertical bar separates alternatives. For example, "gray|grey" matches gray or grey, which can commonly be shortened to "gr(a|e)y".

 

Grouping

Parentheses are used to define the scope and precedence of the operators. For example, "gray|grey" and "gr(a|e)y" are different patterns, but they both describe the set containing gray and grey.

 

Quantification

A quantifier after a character or group specifies how often that preceding expression is allowed to occur. The most common quantifiers are ?, *, and +:  

  • ?  -  The question mark indicates there is 0 or 1 of the previous expression. For example, "colou?r" matches both color and colour. 
  • *  -  The asterisk indicates there are 0, 1 or any number of the previous expression. For example, "go*gle" matches ggle, gogle, google, etc. 
  • +  -  The plus sign indicates that there is at least 1 of the previous expression. For example, "go+gle" matches gogle, google, etc. (but not ggle).  

These constructions can be combined to form arbitrarily complex expressions, very much like one can construct arithmetical expressions from the numbers and the operations +, -, * and /.

// To enable you to use RegularExpressions
// namespace's Classes like Regex & Match.
using System.Text.RegularExpressions;

Sample code

//Check for correct format of txtFname
Match myMatch = System.Text.RegularExpressions.Regex.Match(txtFname.Text, @"^[A-Z][a-zA-Z]*$");
if (!myMatch.Success)
{
      //First Name was incorrect
      ErrorMessage("Invalid First Name", "Message");
      txtFieldName.Focus();
      return;
}

You can down load the running sample.

Login to add your contents and source code to this article
share this article :
post comment
 

Nice Work Done. Its simple and easy to understand

Posted by Waqar Ahmad Mar 12, 2011

Nice artice, simple and straight to the point

Im just startin out with Regex, and I found your article very helpfull

Thanks ,once again

Posted by nkosinathi mathela May 20, 2010

Thank you so much for this article, I've always been too scared to learn Regex because all of those symbols made me feel like I was searching for a needle inside 50 haystacks lol. Much appreciated!!!!! It took me no longer than 15minutes of reading your article to put together my own regex's lol yay!

Posted by jason pezzimenti Mar 25, 2009

I think your application is checking all the enteries in the registry and this is the region why it is slow.You can try in other way...........create a new entry for your application in the registry and map it with the application then try! I think then it will work according to your requirement.

Posted by Sushmita Kumari Nov 07, 2006

Hi sushmita,

I am working on windows application. My application can take user credentials from the registry before loading or if valid credentails are not found, it would open a login screen to take the credentials. The problem is it takes almost double the time in loading if it is taking credentials from the registry than when it is taking information from the user. Please suggest something

Posted by Shweta Mishra Nov 06, 2006
Nevron Gauge for SharePoint
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Become a Sponsor