VBscript regexp - optional submatch problem

Posted on 2004-09-12
Last Modified: 2008-01-09
I have a text string as below

"outside/ (0) -> dmz/"

I want to match the
"outside/" as submatch 1
"" as submatch 2
the "/12" as optional submatch 3
the "(0)" as optional submatch 4

ie all of these four strings would be matched by the pattern

1 "outside/ (0) -> dmz/"
2 "discarded outside/"
3 "discarded outside/ (service)"
4 "discarded outside/2.2.552.2/178 (service)"

I have been trying this pattern,

"outside/"........"IP addresss"...... optional match "/number"......."match anything"......optional match "(xxxx)"

This last optional submatch,  (\(.+?\))? doesn't match the "(0)" portion. If I make the last match required, ie
Then this works fine with the three strings with this match occurs, but not for string 2 wheres the "(xxxx)" portion is missing

What do I need to change in the pattern?



Question by:Dave Brett
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
LVL 50

Author Comment

by:Dave Brett
ID: 12041693
This works but it is a little clumsy


any improvements?

Expert Comment

ID: 12046970
Did you tried


LVL 50

Author Comment

by:Dave Brett
ID: 12061493
Hi _kiew

This pattern misses the



Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Accepted Solution

_kiew earned 500 total points
ID: 12061591
You are right. I have just understood why you made your "match anything" subpattern reluctant (to avoid matching the opening paren)
Of course, you don't have to do so if you want to "match anything but opening paren"

This does work (it has been tested with Perl, if your VBScript regex engine behaves differently, then I cannot help you)


(use * instead of + for example nr 2 to match)

Sorry for the untested answer (at least I learned something today ;-)
LVL 50

Author Comment

by:Dave Brett
ID: 12061865
Hi _kiew

Thats very interesting

By replacing my "find anything after the IP and before parens" in my original pattern
with "find anyhting after the IP that is not a paren"
then suddenly the optional parens match is found

But I'm still unclear why my initial attempt failed, any ideas?




Expert Comment

ID: 12068988
With a string matched by your 1st, 2nd and optional 3rd subpattern, one more character is enough for the global match to succeed, then, if the next characters are "(xxxx)" they will be matched, but that is the only case because then "\(.+?\)" subpattern is optional. This is why your examples 3 and 4 were working, I think.

The most important to grab the "(xxxx)" successfully is to make the "find anything after the IP that is not a paren" subpattern greedy (you see, no "?"). A greedy subpattern will match as much as possible, so this one is matching until the first opening parenthesis, and then you can capture the "(xxxx)" with your optional subpattern.
LVL 50

Author Comment

by:Dave Brett
ID: 12070514

> A greedy subpattern will match as much as possible,
I made it non-greedy with the "?" deliberately as otherwise a string such as

"outside/ (0) -> dmz/ (otherstuff)"
would match
rather then

Thanks for your assistance, your pattern is neater than my workaround second post

Welcome to EE :)


LVL 50

Author Comment

by:Dave Brett
ID: 12157887
Hi kiew,

I came back here as I realised that I'd mis-interpreted your last post and talked about something different after your "A greedy subpattern will match as much as possible". Your negated class was a better method than my lazy +  ...... which kind of goes without saying as my orginal method didn't work

One aspect of VBscript RegExp that I hadn't realised that I was missing till I started posted in the Perl TA was the Lookaround. Boy I could do with that functionality.




Featured Post

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

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

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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…
Six Sigma Control Plans

617 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