Solved

VBscript regexp - optional submatch problem

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

"outside/1.1.2.3/12 customermex-148-244-192-88.alestra.net.mx (0) -> dmz/66.193.215.3"

I want to match the
"outside/" as submatch 1
"1.1.2.3" 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/1.1.2.3/12 customermex-148-244-192-88.alestra.net.mx (0) -> dmz/66.193.215.3"
2 "discarded outside/2.2.2.2/service"
3 "discarded outside/2.5.2.2/as (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)"
ie
(outside\/)(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})(\/\d+)?.+?(\(.+?\))?

This last optional submatch,  (\(.+?\))? doesn't match the "(0)" portion. If I make the last match required, ie
(outside\/)(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})(\/\d+)?.+?(\(.+?\))
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?

Cheers

Dave


0
Comment
Question by:Dave Brett
  • 5
  • 3
8 Comments
 
LVL 50

Author Comment

by:Dave Brett
Comment Utility
This works but it is a little clumsy

"(outside\/)(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})(\/\d+)?(.+?(\(.+?\)){1}|.+?)"

any improvements?
0
 
LVL 2

Expert Comment

by:_kiew
Comment Utility
Did you tried

(outside\/)(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})(\/\d+)?[^(]+?(\(.+?\))?

???
0
 
LVL 50

Author Comment

by:Dave Brett
Comment Utility
Hi _kiew

This pattern misses the
"(0)"
portion

Cheers

Dave

 
0
 
LVL 2

Accepted Solution

by:
_kiew earned 500 total points
Comment Utility
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)

(outside\/)(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})(\/\d+)?[^(]*(\(.+?\))?

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

Sorry for the untested answer (at least I learned something today ;-)
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 50

Author Comment

by:Dave Brett
Comment Utility
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?

Cheers

Dave





0
 
LVL 2

Expert Comment

by:_kiew
Comment Utility
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.
0
 
LVL 50

Author Comment

by:Dave Brett
Comment Utility
Thanks

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

"outside/1.1.2.3/12 customermex-148-244-192-88.alestra.net.mx (0) -> dmz/66.193.215.3 (otherstuff)"
would match
(otherstuff)
rather then
(0)

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

Welcome to EE :)

Cheers

Dave
0
 
LVL 50

Author Comment

by:Dave Brett
Comment Utility
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.

Cheers

Dave


0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

728 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