ARTICLE

Magic of Regex using VB.Net

Posted by Sushmita Kumari Articles | General November 10, 2006
This article is all about Regex which shows different functionalities of the RegularExpression Class with sample code.
Download Files:
 
Reader Level:

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

Dim myMatch As Match = System.Text.RegularExpressions.Regex.Match(txtFname.Text, "^[A-Z][a-zA-Z]*$")

        If Not myMatch.Success Then

            'First Name was incorrect

            ErrorMessage("Invalid First Name", "Message")

            txtFname.Focus()

            Return

        End If

You can down load the running sample.

NOTE: THIS ARTICLE IS CONVERTED FROM C# TO VB.NET USING A CONVERSION TOOL. ORIGINAL ARTICLE CAN BE FOUND ON C# CORNER (http://www.c-sharpcorner.com/). 

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

good job

Posted by carlos roberto de paula junior May 19, 2009

\ ,$,^ and , can u please difine the purpose of these operators and is there any effect of operator precedence plz tell me or giv me some url where this information is present rest of ur article is preety fine

Posted by mian ghous May 28, 2007
Nevron Diagram
Become a Sponsor
PREMIUM SPONSORS
  • 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. Visit DynamicPDF here
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Team Foundation Server Hosting
Become a Sponsor