Regular Expression Conditionals (?(if)then|else) in VBA?

L

Lazzaroni

Does the VBScript_RegExp_55.RegExp object support conditionals?

According to http://www.regular-expressions.info/refflavors.html .NET allows
for conditionals using the (?(if)then|else) structure, and I thought that the
VBScript RegExp object used the .NET flavor of RegEx. But when I try to use
it, the object returns an error message.

When I try to do the following:
objRegExp.Pattern = “^(<)?test(?(1)>|)$â€
I get the following error message:
Method 'Test' of object 'IRegExp2' failed

I am trying to use RegEx conditionals to ensure that a string with an
opening bracket has a closing bracket. So the RegEx above would accept “testâ€
or “<test>â€, but it would not accept “<test†or “test>â€.

I have looked all over for documentation on the VBScript=RegExp_55.RegExp
flavor of RegEx, but have had little success.

Any help would be greatly appreciated.

Thank you.
 
R

Ron Rosenfeld

Does the VBScript_RegExp_55.RegExp object support conditionals?

According to http://www.regular-expressions.info/refflavors.html .NET allows
for conditionals using the (?(if)then|else) structure, and I thought that the
VBScript RegExp object used the .NET flavor of RegEx. But when I try to use
it, the object returns an error message.

When I try to do the following:
objRegExp.Pattern = “^(<)?test(?(1)>|)$”
I get the following error message:
Method 'Test' of object 'IRegExp2' failed

I am trying to use RegEx conditionals to ensure that a string with an
opening bracket has a closing bracket. So the RegEx above would accept “test”
or “<test>”, but it would not accept “<test” or “test>”.

I have looked all over for documentation on the VBScript=RegExp_55.RegExp
flavor of RegEx, but have had little success.

Any help would be greatly appreciated.

Thank you.

The VBScript regex object uses the Javascript flavor.
--ron
 
R

Rick Rothstein

Without using regular expressions, does this do what you want?

If Variable Like "[!<]*[!>]" or Variable Like "<*>" Then
 
L

Lazzaroni

Rick:

That is correct, but within a larger string. I am looking for unclosed
brackets using a data validation application that uses rules written in
RegEx. It checks the contents of cells in Excel, based on a unique column
identifier.

Thanks.

Thomas
 
L

Lazzaroni

Ron:

Too bad it doesn't use the .NET flavor, which is much more robust.

Thanks.
 
R

Rick Rothstein

Okay, but if I understand your larger goal (checking to ensure multiple
left/right brackets in a text string are paired), this (again, non-RegEx)
code might work for you...

If UBound(Split(Variable, "<")) = UBound(Split(Variable, ">")) And _
Not Variable Like "*<[!>]<*" And Not Variable Like "*<[!>]<*" Then
 
R

Ron Rosenfeld

Ron:

Too bad it doesn't use the .NET flavor, which is much more robust.

Thanks.

I'm not sure exactly what you want to do, but

"<test>|(^|[^<])test(?!>)"

might match what you wrote for a specific string, so long as it doesn't include
embedded delimiters.

--ron
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top