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