Solved

VBScript RegExp Object Pattern

Posted on 2015-02-10
21
93 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 51

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 51

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
 
LVL 26

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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 51

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 51

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 51

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

759 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now