Link to home
Start Free TrialLog in
Avatar of Marc Davis
Marc DavisFlag for United States of America

asked on

Regular Expression lookahead with "else" in VBScript.

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
ASKER CERTIFIED SOLUTION
Avatar of ddrudik
ddrudik
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Marc Davis

ASKER

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!
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

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?
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?
to test for @ and / :
regEx.Pattern = "(?=.*@)(?=.*/)"
Set Test = regEx.Test(teststring)
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?

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?
I think I may have it...still testing other scenarios.

Will reporting back confirmed findings.
To test if the string does not contain @ or /:
regEx.Pattern = "(?=^(?:(?![@/])[\S\s])*$)"
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.
That is certainly supported by vbscript, I tested that in my ASP regex tester script I wrote.
My bad...it's the conditional subpatterns. the (?(?!... and the (?(?=...

My apologizies.
You are correct, conditional subpatterns unfortunately are not supported in vbscript.  (?: is a non-capturing group.
ddrudik,

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

Very much appreciated.
Thanks for the question and the points.