XML technologies / Regular expressions / .NET / Using regular expressions in .NET / Overview

(Excerpt from "Regular expressions" by Jeffrey E. F. Friedl)

You get the most from .NET’s regex package by using its comprehensive, accessible class structure. For an overview, here is a complete console program implementing a simple match with explicit objects:

Option Explicit On ' Für reguläre Ausdrücke nicht nötig, aber eine sehr gute
Option Strict On   ' Gewohnheit.
' Regex-Klassen einbinden.
Imports System.Text.RegularExpressions
Module SimpleTest
Sub Main()
    Dim BeispielString as String = "Ganz kurz 1mal testen."
    Dim R as Regex = New Regex("\d+\w+")     'Muster kompilieren.
    Dim M as Match = R.match(BeispielString) 'Auf Suchstring anwenden.
    If not M.Success
        Console.WriteLine("Kein Treffer")
    Else
        Dim MatchedText as String  = M.Value 'Resultatwerte abfragen ...
        Dim MatchedFrom as Integer = M.Index
        Dim MatchedLen  as Integer = M.Length
        Console.WriteLine("Treffer [" & MatchedText & "]" & _
                          " nach Position " & MatchedFrom.ToString() & _
                          ", Länge " & MatchedLen.ToString() & " Zeichen.")
    End If
End Sub
End Module

Running the program from the command line applies ˹\d+\w+˼ to the sample text and prints this line:

Treffer [1mal] nach Position 10, Länge 4 Zeichen.

 

Importing the regex namespace

Note the line ‘Imports System.Text.RegularExpressions’ at the program’s start. Every VB program using Regex objects must include the regex package this way.

The equivalent C# statement is:

using System.Text.RegularExpressions; // Syntax für C#

This example uses basic, low-level Regex objects. The two statements performing the actual regex work,

Dim R as Regex = New Regex("\d+\w+") 'Muster kompilieren.
Dim M as Match = R.match(BeispielString) 'Auf Suchstring anwenden.

can also be combined into one line:

Dim M as Match = Regex.Match(BeispielString, "\d+\w+") 'Muster auf String anwenden.

The combined version is easier to use, requires less typing and leaves fewer objects to remember. It is slightly less efficient, however (see Caching regular expressions). The next pages first use the basic objects, then convenience functions such as static function Regex.Match when appropriate.

In later examples, I generally omit the following three lines:

Option Explicit On
Option Strict On
Imports System.Text.RegularExpressions

The VB examples in earlier chapters may be instructive. They appear in Regular expressions in VB and other .NET languages, Search and replace in VB.NET, A link checker in VB.NET, Processing CSV files with VB.NET and Benchmarks with VB.NET.

  

 

 

 

German edition copyright © 2008 by O’Reilly Verlag GmbH & Co. KG
You may print the online version for private use.
Otherwise, this chapter from "Reguläre Ausdrücke" is subject to the same terms as the bound edition: the work and all its parts are protected by copyright. All rights reserved, including reproduction, translation, microfilming, storage and processing in electronic systems.

O’Reilly Verlag GmbH & Co. KG, Balthasarstr. 81, 50670 Köln

back to top