Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > VB Script

Vista - RegEx to return False if input is invalid.

Reply
 
Old 09-11-2008   #1 (permalink)
RemyMaza


 
 

RegEx to return False if input is invalid.

This code needs to be able to know whether the InputBox is in the
right format. If not the right format how do I have the RegEx
determine it's a False value? The code works great when the right
number format has been typed, but I can't get the last For Each Loop
to work in my favor. The number format is 00-00-0000.


'~~~~~~~~~~~~~~~~~~~~~~~~~~~Code~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


Option Explicit

'Declare our variable
Dim objRegExpr, objMatch, objMatch1
Dim strNum
'Declare a variable to hold our collection of Matches
Dim colMatches

'Create an instance of the regexp object
Set objRegExpr = New regexp

objRegExpr.Pattern = "[0-9]{2}-[0-9]{2}-[0-9]{4}"
objRegExpr.Global = True
objRegExpr.IgnoreCase = True

'InputBox for Job#
strNum = InputBox("Type # of Job")


'Now, Execute the regular expression search
Set colMatches = objRegExpr.Execute(strNum)
MsgBox colMatches.Count & " matches found..."
For Each objMatch in colMatches
MsgBox objMatch.Value
Next

For Each objMatch1 in colMatches <> True
MsgBox objMatch1.Value
Next

'~~~~~~~~~~~~~~~~~~~~~~~~~End~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


Thanks,
Matt

My System SpecsSystem Spec
Old 09-11-2008   #2 (permalink)
ekkehard.horner


 
 

Re: RegEx to return False if input is invalid.

RemyMaza schrieb:
Quote:

> This code needs to be able to know whether the InputBox is in the
> right format. If not the right format how do I have the RegEx
> determine it's a False value? The code works great when the right
> number format has been typed, but I can't get the last For Each Loop
> to work in my favor. The number format is 00-00-0000.
[...]
Assuming, the user should enter *one* Type code
Quote:

> objRegExpr.Pattern = "[0-9]{2}-[0-9]{2}-[0-9]{4}"
the pattern should use ^ and $ to indicate that there shouldn't be
anything before or after the Type code
Quote:

> objRegExpr.Global = True
should be False (default)
Quote:

> objRegExpr.IgnoreCase = True
makes no sense for numerical input
[...]
Quote:

> 'Now, Execute the regular expression search
use .Test to check if input matches pattern or not, that way you
get your boolean without further work
[...]
Quote:

> For Each objMatch1 in colMatches <> True
That's not VBScript; you'll have to write your own compiler
/interpreter for this new RemyMaza language.

Use code like this

Dim aTests : aTests = Array( _
"12-34-5678", "", " 00-00-0000 ", "12-3O-4321" _
)
Dim oRE : Set oRE = New RegExp
oRE.Pattern = "^\d{2}-\d{2}-\d{4}$"
Dim sTest
For Each sTest In aTests
WScript.StdOut.Write "sTest: |" & sTest & "| => "
sTest = Trim( sTest )
If oRE.Test( sTest ) Then
WScript.StdOut.WriteLine "good."
Else
WScript.StdOut.WriteLine "bad."
End If
Next

output:

RegExpDemo aufgerufen.
sTest: |12-34-5678| => good.
sTest: || => bad.
sTest: | 00-00-0000 | => good.
sTest: |12-3O-4321| => bad.
done: 0

to see what I mean/experiement with RegExps.


My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
HELP! fileExist function always return 'FALSE' VB Script
Re: how to capture "return $false" PowerShell
Chinese Traditional Input using Phonetic Input Vista General
Test-Path '' -IsValid does not return False but fails. Is this OK? PowerShell
Weird [regex]::Matches return value PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46