VBScript RegExp Object Pattern

I need some vbscript code to check a string to ensure it only has A-Z, 0-9, period or underscore characters in it.

I found VBScript RegExp Object, but I cannot figure out what to use for my pattern.

Any help would be greatly appreciated.
BFanguyAsked:
Who is Participating?
 
Bill PrewConnect With a Mentor Commented:
For quote you can also do the following.  Within a quoted string, pairs of double quotes are always replaced by a single double quote.

"^[a-zA-Z0-9_./'*,#+ ""\\\-]*$"

~bp
0
 
Bill PrewCommented:
Sure, are you intending to do this in a stand alone VBS script that does other things, or as part of a web page?

~bp
0
 
BFanguyAuthor Commented:
stand alone vbs script.

something simple like

str = "12aB.3_4"
Set myRegExp = New RegExp
myRegExp.IgnoreCase = True
myRegExp.Pattern = "^[A-Z0-9]{5}\d$"  ' i know this is not my needed pattern, but something like this
isValid = myRegExp.Test(str)    ' returns True in this case
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
Bill PrewCommented:
Give this a try:

isValid will follow normal boolean practice in VBS, so will be -1 for True, and 0 for False.

str = "12aB.3_4"
Set myRegExp = New RegExp
myRegExp.IgnoreCase = True
myRegExp.Pattern = "^[a-zA-Z0-9_.]*$"
isValid = myRegExp.Test(str)
Wscript.Echo isValid

Open in new window

~bp
0
 
MacroShadowConnect With a Mentor Commented:
Try this pattern:
[A-Za-z0-9]{4}.\d_\d

Open in new window

0
 
BFanguyAuthor Commented:
thanks, i also need to add a dash, how would i add it to the pattern?
0
 
BFanguyAuthor Commented:
and a quote
0
 
BFanguyAuthor Commented:
macroshadow,

what does the {4} do?
0
 
Terry WoodsIT GuruCommented:
Including underscore, single quote and dash:
 myRegExp.Pattern = "^[A-Z0-9_'\-]+$"

Open in new window

Change the + to a * character if an empty string is ok.

The {4} means match exactly four of the previous character.
0
 
BFanguyAuthor Commented:
thanks, but was looking for double quote  "
0
 
Terry WoodsConnect With a Mentor IT GuruCommented:
In case you're interested, the pattern:
[A-Za-z0-9_]
can be abbreviated to just \w or [\w] so provided that you're happy to accept lower case your pattern could be simplified to:
"^[\w\-\"]+$"
0
 
BFanguyAuthor Commented:
and the dash does not work, it thinks its a range  like a-z
0
 
BFanguyAuthor Commented:
sorry, for piecemealing this, but i am finding that i need more than just the couple of special characters.
so what i need is the /w and.
Asterisk, comma, quotes, dash, slash, pound, plus, single quote, period, underscore
0
 
BFanguyAuthor Commented:
sorry, backslash not slash

the quotes " and the dash - are the ones i am having trouble with.

if i use the \" how does it know if i am including the backslash and the quotes or if this is for the quote.

again, thank you for your help
0
 
BFanguyAuthor Commented:
also need a space included.

As far as i can tell this works for everything but the dash and the double quotes
"^[a-zA-Z0-9_./'*,#+ ]*$"
0
 
Bill PrewCommented:
Backslash is always treated as an "escape" character, the only way to get an actually backslash is to escape it, so \\.

Take a look at these sites for Regex info:

http://www.tutorialspoint.com/vbscript/vbscript_reg_expressions.htm
http://www.regular-expressions.info/vbscriptexample.html

~bp
0
 
Bill PrewCommented:
Try this:

"^[a-zA-Z0-9_./'*,#+ \\\-]*$"
0
 
BFanguyAuthor Commented:
thanks, but i think i am still missing the " (quotes) character.

i.e. it fails on this string value:
FLWS 1/2" .78" .65"
0
 
BFanguyAuthor Commented:
can i use a Chr(34) somehow?
0
 
BFanguyAuthor Commented:
this works....

"^[a-zA-Z0-9_./'*,#+" & Chr(34) & " \\\-]*$"
0
 
BFanguyAuthor Commented:
Thanks guys
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.