VBScript Find Hexadecimal Numbers

From Regex Regular Expression Encyclopedia

Jump to: navigation, search

This recipe will limit the input to numbers and letters that are valid for hexadecimal representations.

[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 = "^[0-9a-f]+$"
re.IgnoreCase = True
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
^ the beginning of the line, followed by . . .
[0-9a-f] a character class that matches the digits zero through nine and the letters
\s a through f . . .
+ found one or more times . . .
$ the end of the line.

Alternatively, you can replace 0-9 in the character class with \d.

Personal tools