Solved

VBScript RegExp Object Pattern

Posted on 2015-02-10
21
104 Views
Last Modified: 2015-02-10
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.
0
Comment
Question by:BFanguy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 13
  • 5
  • 2
  • +1
21 Comments
 
LVL 55

Expert Comment

by:Bill Prew
ID: 40601128
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
 

Author Comment

by:BFanguy
ID: 40601143
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
 
LVL 55

Expert Comment

by:Bill Prew
ID: 40601270
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 27

Assisted Solution

by:MacroShadow
MacroShadow earned 50 total points
ID: 40601274
Try this pattern:
[A-Za-z0-9]{4}.\d_\d

Open in new window

0
 

Author Comment

by:BFanguy
ID: 40601428
thanks, i also need to add a dash, how would i add it to the pattern?
0
 

Author Comment

by:BFanguy
ID: 40601432
and a quote
0
 

Author Comment

by:BFanguy
ID: 40601464
macroshadow,

what does the {4} do?
0
 
LVL 35

Expert Comment

by:Terry Woods
ID: 40601497
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
 

Author Comment

by:BFanguy
ID: 40601500
thanks, but was looking for double quote  "
0
 
LVL 35

Assisted Solution

by:Terry Woods
Terry Woods earned 50 total points
ID: 40601502
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
 

Author Comment

by:BFanguy
ID: 40601506
and the dash does not work, it thinks its a range  like a-z
0
 

Author Comment

by:BFanguy
ID: 40601514
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
 

Author Comment

by:BFanguy
ID: 40601527
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
 

Author Comment

by:BFanguy
ID: 40601536
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
 
LVL 55

Expert Comment

by:Bill Prew
ID: 40601541
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
 
LVL 55

Expert Comment

by:Bill Prew
ID: 40601545
Try this:

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

Author Comment

by:BFanguy
ID: 40601552
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
 

Author Comment

by:BFanguy
ID: 40601555
can i use a Chr(34) somehow?
0
 

Author Comment

by:BFanguy
ID: 40601561
this works....

"^[a-zA-Z0-9_./'*,#+" & Chr(34) & " \\\-]*$"
0
 
LVL 55

Accepted Solution

by:
Bill Prew earned 400 total points
ID: 40601598
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
 

Author Comment

by:BFanguy
ID: 40601609
Thanks guys
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I met Paul Devereux (@pdevereux) today when I responded to his tweet asking “Anybody know how to automate adding files from disk to a folder in #outlook  ?”.  I replied back and told Paul that using automation, in this case scripting, to add files t…
Introduction During my participation as a VBScript contributor at Experts Exchange, one of the most common questions I come across is this: "I have a script that runs against only one computer. How can I make it run against a list of computers in …
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question