Monday, December 05, 2005

Regular Expressions in .Net

Regular Expressions

Regular expressions have been widely popular in languages such as PERL and AWK and have been utilized for pattern matching, text manipulation and text searching. These languages are specifically is known for its advanced pattern matching features. Dot Net regular expressions are based on that of Perl and are compatible with Perl 5 regular expressions.

To begin with, they are not as complex as they look, especially if you start experimenting with them. I would recommend that you download a tool such as Expresso (http://www.ultrapico.com/), to become familiar with regular expressions.

Regular Expression Elements

Some of the commonly used regular expression elements are:

^

Matches start of input

$

Matches end of input

.

Matches any character except new line

|

OR

*

Match the preceding expression 0 or more number of times

+

Match the preceding expression 1 or more number of times

?

Match the preceding expression 0 or 1 number of times

()

Logical group / sub-expression (capture as auto number group)

(?(exp))

Named capture group

(?=exp)

Match any position preceding a suffix exp

(?<=exp)

Match any position following a prefix exp

(?!exp)

Match any position after which exp is not found

(?

Match any position before which exp is not found

[…]

List of characters to match

[^expression]

Not containing any of the specified character

{n} or {n. m}

Quantifier (Match exact number or range of instances)

(?(exp (yes|no))

If expression (exp) is true match yes part else no part

\

Escape character (to match any of the special characters)

\w

Match any word character

\W

Match any non-word character

\s

Match any white space character

\S

Match any non-white space character

\d

Match any numeric digit

\D

Match any numeric digit

\b

Match a backspace if in character matching mode ([]).

Otherwise match the position at beginning or end of a word

\t

Match tab

\r

Match carriage return

\n

Match line feed


The following are matching substitutions:

num

Substitute last substring matched by group number num

${name}

Substitute last substring matched by group name

$&

Substitute a copy of entire text itself

$`

Substitute all the text of the input string before match

$’

Substitute all the text of the input string after match

$+

Substitute last matched group

$_

Substitute input string

$$

Substitute literal $


Regular expressions could also be used to find repeating patterns by making use of backreferencing, using which you can name a pattern found and then use that reference elsewhere in expression. This naming of patterns is also useful in case we need to parse a string like free form date or time strings.

Some Example Regular Expressions

  • Match a word - \btest\b
  • Match all 6 letter words - \b\w{6}\b
  • Match all 6 digit numbers - \b\d{6}\b
  • Match any number \b\d+\b

Instead of giving loads of examples here, I suggest that you download Expresso and check its analyzer view for detailed analysis of the regular expression.

Regular Expressions in .Net

As already discussed, .Net regular expressions are based on that of Perl and are compatible with Perl 5 regular expressions. Dotnet contains a set of powerful classes that makes it even easier to use regular expressions. The classes are available in the System.Text.RegularExpressions namespace.

How to validate an input string in .Net

  • Create a Regex object ‘RegexObj’
  • Call RegexObj.IsMatch (subjectString ), which will return a Boolean showing validity of input string

How to perform regular expression substitution (search and replace) in .Net

  • Create a Regex object ‘RegexObj’
  • Call RegexObj.Replace ( subjectString, replaceString ), which will return a Boolean showing validity of input string

How to parse an input string in .Net

  • Create a Regex object ‘RegexObj’, make sure to name the expressions
  • Call RegexObj.Match ( subjectString ), which will return a list of matches in the input string as per the match regular expression
  • Iterate through the matches to perform post parsing

Free form time parsing function in DotNet

For an example, I have developed a simple free format time parser. I have provided the code and details in this code project article.

References and Further

Saturday, October 22, 2005

XML (XMLType) inside PL/SQL Oracle 9/10

The XMLType is an OO XML aware data type. It can be used in columns or in PL/SQL just like VARCHAR2 or DATE. XMLType has member functions that allow access to data using XPath.

A quick example extracting a specific value from an XML varchar2 string:
DECLARE
v VARCHAR2(32000) := 'ABC';
x XMLType;
BEGIN
x := XMLType(v);
DBMS_OUTPUT.put_line(
x.extract('/DATA/LINE[1]').getStringVal()
);
DBMS_OUTPUT.put_line(
x.extract('/DATA/LINE[1]/text()').getStringVal()
);
END;

Tuesday, September 27, 2005

WinFX Developer Center: InfoCard: The Laws of Identity

WinFX Developer Center: InfoCard: The Laws of Identity: "The Laws of Identity"

Wednesday, September 21, 2005

Fields to Properties (Visual Studio.Net)

Yes, we miss Automatic property generation from the Fields of the classes in the Visual Studio.net IDE.
Following is the sample code of the Macro, I am telling here which I used for myself.
This member variable to Property generation is not generic and is very difficult to be. The idea is that I demonstrate to do it my way and you can customize it for your needs.

Code is in the Visual Basic.Net
--------------------------------------------------
NEED:

Public Class AClass

#Region "Members"
Private strStaffId As String
Private strStaffName As String
#End Region

End Class

-->
Public Class AClass

#Region "Members"
Private strStaffId As String
Public Property StaffId() As String
Get
Return strStaffId
End Get
Set(ByVal Value As String)
strStaffId = Value
End Set
End Property

Private strStaffName As String
Public Property StaffId() As String
Get
Return strStaffId
End Get
Set(ByVal Value As String)
strStaffId = Value
End Set
End Property

#End Region

End Class

--------------------------------------------------

The first step in using a macro to generate code is to open the Macros IDE, add a new module, a macro, and stub out the code template.

1. To create a new macro, open Visual Studio .NET—I am using VS.NET 2003, but the example works in version 1—and select Tools|Macros|Macros IDE
2. In the Macros Project Explorer click on the MyMacros project, right-clicking Add|Add Module from the Project Explorer context menu
3. Add a public subroutine named WriteProperty to the module

So, In my case it is like:
File:- MyModule
Imports EnvDTE
Imports System
Imports System.Diagnostics

Public Module MyModule

Private Cr As String = Environment.NewLine

Private mask As String = _
"Public Property {0}() As {1}" + Cr + _
" Get" + Cr + _
" Return {2}" + Cr + _
" End Get" + Cr + _
" Set(ByVal Value As {1})" + Cr + _
" {2} = Value" + Cr + _
" End Set" + Cr + _
" End Property" + Cr

Public Sub WriteProperty()
Dim Selection As TextSelection = DTE.ActiveDocument.Selection

Dim FieldName As String
Dim PropertyName As String
Dim PropertyType As String

FieldName = Selection.Text
PropertyName = FieldName.Substring(3) 'skip strUserName -> UserName

Selection.EndOfLine()
Selection.WordLeft(True)
PropertyType = Selection.Text
Selection.EndOfLine()
Selection.NewLine(2)

Dim vp As VirtualPoint = Selection.ActivePoint
Selection.Insert(String.Format(mask, PropertyName, PropertyType, FieldName))
End Sub
End Module
-----------------------------------------------------------------------

Now, You can customize the VisualStudio environment and put a button on a toolbar and that button can call our macro. You can also assign a hot key to call this code.
To generate the property for the field, You only have to select the field and it will generate its code for the corresponding property.

When done, you only need to edit this code according to your needs.

bye
:)

Saturday, September 03, 2005

XML documentation in Visual Basic.NET applications

By default VB.Net in VS.net is missing support for automated generation of the documentation from with in the source, Like Recommended Tags for Documentation Comments.
Here 'VBCommenter' PowerToy from the Got Dot Net Web site can help us. With VBCommenter plug-in, they can create XML documentation as they are writing code.
To use this facility, it's first necessary to install VBCommenter. The download is a ZIP file that includes a standard Setup.exe file with an accompanying .msi file; it's trivial to unpack and install and should take no more than two minutes. Configure VBCommenter settings from the Tools VBCommenter Options menu. Here, make sure the checkboxes for both "Create .xml files when projects are built" and "Insert XML comments in source" are checked.
Once the plug-in is running and configured, simply key three apostrophes into the Start Page for any class definition, property, member variable, or method.

Because documenting as you go is the best way to make sure all the nuances of the code are captured, this technique (and the VBCommenter tool) are highly recommended for those seeking an easy way to add structured documentation to their work.

For a bit advance work, VBCommenter can be customized as described here : Customizing The VBCommenter Power Toy