Visual Basic .NET replace tab characters
From Regex Regular Expression Encyclopedia
This wiki is for replacing tab characters in a string with a different character using Visual Basic .NET. In this recipe, we use a pipe (|) to replace the tab.
[edit] code
Imports System Imports System.IO Imports System.Text.RegularExpressions Public Class Recipe Private Shared _Regex As Regex = New Regex("\t") Public Sub Run(ByVal fileName As String) Dim line As String Dim newLine As String Dim sr As StreamReader = File.OpenText(fileName) WORDS AND TEXT line = sr.ReadLine While Not line Is Nothing newLine = _Regex.Replace(line, "|") Console.WriteLine("New string is: '{0}', original was: '{1}'", _ newLine, _ line)) line = sr.ReadLine End While sr.Close() End Sub Public Shared Sub Main(ByVal args As String()) Dim r As Recipe = New Recipe r.Run(args(0)) End Sub End Class
[edit] How It Works
Breaking the recipe down yields simply the following:
| Regular Expression | Description |
|---|---|
| \t | is a tab, replaced by . . . |
| | | a pipe character. |
By default, the regular expression object in the .NET Framework replaces each occurrence of the text that matches the search regex. However, the VBScript and JavaScript regular expres- sion objects work differently because an option must be specified to replace each match. You can find more about these options in the “Syntax Overview” section of this book.
[edit] Variations
Since this is such a simple recipe, it has an extensive number of variations. You could replace the character class representing a tab with other character classes, especially the \s character class. The JavaScript variation /\s/g would replace each instance of whitespace with |.
One variation on the previous recipe is to use a qualifier after the character class to replace more than one instance of a tab at once. For instance, if you want to replace two tabs, you could use something such as /\t{2}/g in the JavaScript recipe.
