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
LVL 1
davismAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ddrudikCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
davismAuthor Commented:
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
ddrudikCommented:
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
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

davismAuthor Commented:
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
davismAuthor Commented:
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
ddrudikCommented:
to test for @ and / :
regEx.Pattern = "(?=.*@)(?=.*/)"
Set Test = regEx.Test(teststring)
0
davismAuthor Commented:
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
davismAuthor Commented:
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
davismAuthor Commented:
I think I may have it...still testing other scenarios.

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

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

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

Very much appreciated.
0
ddrudikCommented:
Thanks for the question and the points.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Regular Expressions

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.