Solved

VBScript RegExp Object Pattern

Posted on 2015-02-10
21
95 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 52

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 52

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 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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 52

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 52

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 52

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

When it comes to writing scripts for a Client/Server computing environment it is essential to consider some way of enabling the authentication functionality within a script. This sort of consideration mainly comes into the picture when we are dealin…
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…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

912 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

24 Experts available now in Live!

Get 1:1 Help Now