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
Solved

VBScript RegExp Object Pattern

Posted on 2015-02-10
21
99 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
  • 13
  • 5
  • 2
  • +1
21 Comments
 
LVL 53

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 53

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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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 53

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 53

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 53

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Well hello again!  Glad to see you've made it this far without giving up.  In this, the fourth installment of my popular series, I'm going to cover functions and subroutines, what they are, and why they are useful.  Just in case you stumbled onto th…
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

856 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