Solved

VBScript RegExp Object Pattern

Posted on 2015-02-10
21
101 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 54

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 54

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 54

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 54

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 54

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

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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VBA & VBSCRIPT: re-adapt log file 13 41
VB Script: restart service function 30 40
VB Script: Correction backup files and folders v3 11 31
MS ACCESS 2010 Clear Form Button 4 63
This is an addendum to the following article: Acitve Directory based Outlook Signature (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_24950055.html) The script is fine, and works in normal client-server domains…
This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

726 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