VBScript Find Blank Lines

From Regex Regular Expression Encyclopedia

Jump to: navigation, search

You can use this recipe for identifying blank lines in a file. Blank lines can contain spaces or tabs, or they can contain a combination of spaces and tabs. Variations on these expressions can be useful for stripping blank lines from a file.

[edit] code

Dim fso,s,re,line,lineNbr
Set fso = CreateObject("Scripting.FileSystemObject")
Set s = fso.OpenTextFile(WScript.Arguments.Item(0), 1, True)
Set re = New RegExp
re.Pattern = "^\s*$"
lineNbr = 0
Do While Not s.AtEndOfStream
    line = s.ReadLine()
    lineNbr = lineNbr + 1
    If re.Test(line) Then
        WScript.Echo "Found match: '" & line & "' at line " & lineNbr
    End If
Loop
s.Close

[edit] How It Works

This recipe uses a simple character class to limit the input to digits and the letters a through f:

Regular Expression Description
^ starts the beginning of the line, followed by . . .
\s any whitespace character (a tab or space) . . .
* zero or more times, followed by . . .
$ the end of the line.

[edit] Variations

In addition to completely blank lines, this expression also matches lines that have only tabs or spaces in them. It does this by using the \s character class, which matches a tab or a space.

Here's the expression broken down into parts:

Personal tools