Solved

Regular Expression lookahead with "else" in VBScript.

Posted on 2008-10-23
16
773 Views
Last Modified: 2012-05-05
All,

I have a question with respect to a regular expression in VBScript. I'm trying to do a look ahead and if TRUE perform some item and if false perform another item and then just do a substitution at the end regardless of true or false at that point.

I'm trying to do something like:

((?=([/@])(([\s\""\d:]?)|([\s\""\d:]))))

Basically, if the string being evaluated has a "/" and a "@" then I want it to perform ([\s\""\d:]?) otherwise
([\s\""\d:]). The difference between the latter 2 statements is that I allow for 0 to a 1 of space at the end (the "?") or the alternative is that I do not allow any space. (nothing there).

I'm doing a lookahead for the "/" and "@" but how to I get it to do the TRUE ELSE/OR FALSE. Notice I do have the "|" in there to separate the 2 statements.

I am doing this in VBScript.

Any ideas or assistance would be greatly appreciated.

Thanks
0
Comment
Question by:davism
  • 9
  • 7
16 Comments
 
LVL 27

Accepted Solution

by:
ddrudik earned 250 total points
ID: 22793178
Conditional subpatterns (to my knowledge) are not supported in vbscript regex.
In PHP PCRE that would appear as something like:
(?(?=.*[/@])[\s\""\d:]?|[\s\""\d:])

But in your case you would need to create a regexp object, perform a .test operation for your ?=.*[/@] and then based on the true/false of that test, perform a second test with one of two patterns.
0
 
LVL 1

Author Comment

by:davism
ID: 22793218
Man, I was hoping that wasn't the case. I'm used to Perl and I was able to do it quite easily with Perl. NOTING the first ? like in your example. I tried that in VBScript but I kept receiving an error and was hoping there was another way.

I'll let this question sit here so see if somebody else might chime in here with a possible resolution with VBScript.  If not, I will give you the points.

Thanks!
0
 
LVL 27

Expert Comment

by:ddrudik
ID: 22793364
For confirmation, this table shows the options available in the different regex flavors, ECMA is the column that would correspond with JavaScript/VBScript:
http://www.regular-expressions.info/refflavors.html

The operation as I understand it would appear as:
<%

Set regEx = New RegExp

teststring = "<your string>"

regEx.Pattern = "(?=.*[/@])"

Set Test = regEx.Test(teststring)

If Test=True Then

  regEx.Pattern = "[\s\""\d:]?"

Else

  regEx.Pattern = "[\s\""\d:]"

End If

Set Test = regEx.Test(teststring)

%>

Open in new window

0
 
LVL 1

Author Comment

by:davism
ID: 22846494
Actually, I thought this was right but I saw some other things happening.

If I have a date like 10/30/08.

It will convert that date to my replacement value as well (for instance, if my replacement value is "***", I end up with 10***).

I need both a "/" and an "@" on the line hence the ?=[/@]

What am I missing here?
0
 
LVL 1

Author Comment

by:davism
ID: 22847226
I'm either completely brain dead right now or something.

What I'm trying to do is look for 2 characters on the line before I execute:

ddrudik, so using the example:


Set regEx = New RegExp
teststring = "<your string>"
regEx.Pattern = "(?=.*[/@])"
Set Test = regEx.Test(teststring)
If Test=True Then
  regEx.Pattern = "[\s\""\d:]?"
Else
  regEx.Pattern = "[\s\""\d:]"
End If
Set Test = regEx.Test(teststring)

If the teststring equated "Hello there Mary/Tina today is 10/30/08." and I did a replacement with "***" then I end up with

"Hello there Mary*** today is 10***."

So, it's not even seeing the "@" but yet it is still matching.

Why is that and how can I get it so that it must match both of those characters at least 1 time?

(?=.*[/@])+

What am I missing here?
0
 
LVL 27

Expert Comment

by:ddrudik
ID: 22847258
to test for @ and / :
regEx.Pattern = "(?=.*@)(?=.*/)"
Set Test = regEx.Test(teststring)
0
 
LVL 1

Author Comment

by:davism
ID: 22847321
ddrudik,

I thought I tried that. Maybe I missed typed something. I did a copy and paste of what you had to avoid any typo issue and it looks promising.

However, what would be the opposite?

"(?!.*@)(?!.*/)"

Because that doesn't seem to be working.

That I'm trying to do is mask an oracle password in an oracle connection string on a command line.

so davism/mypassword@myserver

Think it might be better to deal with this on a word boundary? That way the whole thing of:

davism/mypassword@myserver is viewed as a word and the "/" and "@" are within that word? So basically check for those values within that word?

What are your thoughts?

0
 
LVL 1

Author Comment

by:davism
ID: 22847367
Oh, I should clarify because this information may be on a line with other text so something like:

"The login of davism/mypassword@myserver occurred in Oracle on 10/30/08."

or the opposite of

"The login of user: davism with password=mypassword occurre in MySql on 10/30/08."

Make better sense now? This is why I'm thinking to check the Oracle on a word boundary with the "/" AND "@" as compared to NOT seeing them in the line.

Hope this makes sense. The more I'm thinking the more the word boundary or (space separated bulk value) with the presence of a "/" and "@" might work better. The lookahead would help make that determination for Oracle but maybe that lookahead is on a word boundary.

Thoughts?
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 1

Author Comment

by:davism
ID: 22847578
I think I may have it...still testing other scenarios.

Will reporting back confirmed findings.
0
 
LVL 27

Expert Comment

by:ddrudik
ID: 22850077
To test if the string does not contain @ or /:
regEx.Pattern = "(?=^(?:(?![@/])[\S\s])*$)"
0
 
LVL 1

Author Comment

by:davism
ID: 22850735
Remember, I can't use the (?:(?!... or the (?:(?=... that isn't covered in the VBScript ability.

The start and end of line ^ and $ covers the line, absolutely but it's not needed in this context.

As mentioned, I *think* I have it but still testing out. So far it's looking good and the information you provided on the @ AND / check is working out. I really don't know what I did. Most likely hosed up parethesis. :-)

Let me do some more double-checking and testing with what I have and if all works out be assured you will get the points. :

Thanks and information forthcoming on results.
0
 
LVL 27

Expert Comment

by:ddrudik
ID: 22850795
That is certainly supported by vbscript, I tested that in my ASP regex tester script I wrote.
0
 
LVL 1

Author Comment

by:davism
ID: 22850955
My bad...it's the conditional subpatterns. the (?(?!... and the (?(?=...

My apologizies.
0
 
LVL 27

Expert Comment

by:ddrudik
ID: 22851049
You are correct, conditional subpatterns unfortunately are not supported in vbscript.  (?: is a non-capturing group.
0
 
LVL 1

Author Closing Comment

by:davism
ID: 31509524
ddrudik,

Thanks much and sorry for the delay on awarding points. I had to test throughly to ensure everything!

Very much appreciated.
0
 
LVL 27

Expert Comment

by:ddrudik
ID: 22882749
Thanks for the question and the points.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

746 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

9 Experts available now in Live!

Get 1:1 Help Now