VBScript replace tab characters
From Regex Regular Expression Encyclopedia
This wiki is for replacing tab characters in a string with a different character using VBScript. In this recipe, we use a pipe (|) to replace the tab.
[edit] code
Dim fso,s,re,line,newstr Set fso = CreateObject("Scripting.FileSystemObject") Set s = fso.OpenTextFile(WScript.Arguments.Item(0), 1, True) Set re = New RegExp re.Pattern = "\t" re.Global = true Do While Not s.AtEndOfStream line = s.ReadLine() newstr = re.Replace(line, "|") WScript.Echo "New string '" & newstr & "', original '" & line & "'" Loop s.Close
[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.
