Saturday, January 29, 2011

Using Regular Expression - VB.NET

What is Regular Expression?
  1. It is great technique to search through texts.
  2. Supported by many programming languages.
  3. It use textual formulas contain some special characters that have special meaning.

  • Imagine that you want to search for emails in a text file or search for titles in websites here comes the role of the Regular Expression optimal solution.

The support of framework for Regular Expression:

  • Like many programming languages, the framework supports the Regular Expression through a set of classes exist in the space of names System.Text.RegularExpressions.


Regex object is used to search through text on a certain formula Declaration for the object is like this:






    Dim r As Regex
    r = New Regex("abc") 

After that comes the role of the Match, which contains the results of the search operation.It will be a good idea to test 'Match.Success' before attempting to read the results: 

    Dim r As New Regex("abc")
    Dim m As Match = r.Match("123abc456")
    
    If m.Success Then
        Console.WriteLine("Found match at position " & m.Index.ToString())
    End If

In the former case the 'Match' will be the first characters group that equal to search pattern (i.e. the first result)
but if we want to bring all search results we use Regex.Matches.MatchCollection


    Dim mc As MatchCollection
    Dim results(20) As String
    Dim matchposition(20) As Integer
    Dim r As New Regex("abc")

    mc = r.Matches("123abc4abcd")

    For i As Integer = 0 To mc.Count - 1
        results(i) = mc(i).Value
        matchposition(i) = mc(i).Index
    Next i       

How to write formulas for the Regular Expression:  

  • Static text:
If you want to search for a word or phrase or a set of characters you can simply write formula
Example:


I Can write In visual Basic, visual c + + and visual foxpro
If we want to search for "visual"; will be three outcomes Search Results for "visual"

  • Search for any character:
We use the point "." To replace any character or symbol
Example:
I Have visual basic6, visual basic8, visual basic9
Search for: "Basic." ; This will result in basic6 and basic8 and basic9
Note: In the previous example, the point is equal to numbers, but can be equal to any character
Note: the point is equal to any character or symbol, including the point itself, but if you want to search for the point itself must be preceded by backslash(\)

  • Search for a character from the character set:
We use brackets [] to search for any character within the brackets
Example:
[chr] at
This formula will result hat or cat or rat
Note: The Regular Expression is case sensitive

* Dashes can be used to specify spans of characters in a class:
[0-9] ==> [0123456789]
[a-z] ==>> [abcdefghigklmnopqrstuvwxyz]
[A-Z] ==>> [ABCDEFGHIJKLMNOPQRSTUVWXYZ]
Can also mix them like this: [a-zA-Z0-9]
Example: To search for color in HTML format can use the following formula
# [0-9A-Fa-f] [0-9A-Fa-f] [0-9A-Fa-f] [0-9A-Fa-f] [0-9A-Fa-f] [0-9A-Fa-f]


* A caret at the left end of a class definition means the opposite: [^0-9]

  • Search for white space:
To search for characters that are not printed using the following formulas:

[\b]  Backspace
\f  Form feed
\n  Line feed
\r  Carriage return
\t  Tab
\v  Vertical tab
  • Character class abbreviations:

\d [0-9]
\D [^0-9]
\w [A-Za-z_0-9]
\W [^A-Za-z_0-9]
\s [ \r\t\n\f]
\S [^ \r\t\n\f]

4 comments:

  1. very good article ..

    ReplyDelete
  2. By Combining the Domain expertise with Low Cost Offshore .NET Development process, CATT Ltd’s Offshore development Model delivers Enhanced Productivity with Quality and Cost Savings. CATT Ltd offers its customers an option of having a Dedicated offshore .NET Developers to augment their IT capabilities by proving access to their Talented professionals and the state-of-the-art infrastructure.

    ReplyDelete
  3. CATT Ltd VB programmers have always displayed excellent entrepreneurial skills and along with their real hands-on programming experience.Our VB developer's experience comprises mostly business Application Development, database application development, Software Development, Custom Web & Software Application Development.

    ReplyDelete