Solved

parenthesis in regular expressions

Posted on 2003-12-10
5
551 Views
Last Modified: 2008-06-05

in perl I can say
"foo:bar" =~/(\w*):(\w*)/;

$firstWord=$1;
$secondWord=$2;


Is there any equivalent with Visual Basic regular expressions?

something like: (I've commented out lines that are guesses)
    Dim re As New RegExp
 '   Dim result As Object
   
    re.Pattern = "(\w*):"
'    result = re.Execute(lbFound)

'debug.print result(1)


0
Comment
Question by:josephfluckiger
5 Comments
 
LVL 1

Accepted Solution

by:
JSheldon earned 125 total points
ID: 9918200
I think what your looking for is matches and match collection.  Here is a good example using that:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/vsobjregexp.asp

For a good reference of Visual Basic Regular expressions pattern property check:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/vspropattern.asp
0
 
LVL 1

Author Comment

by:josephfluckiger
ID: 9918232
cool, that does the trick. sure is wordy compared to Perl, but at least I have functionality I've longed for in VB.
0
 
LVL 1

Expert Comment

by:JSheldon
ID: 9918245
Quite wordy indeed, and IMO not as powerful, but it gets the job done :)
0
 
LVL 9

Expert Comment

by:Dang123
ID: 9920081
Learning.
0
 

Expert Comment

by:LifelongLearner
ID: 11402491

The Replace method can be pressed into keeping only what you want as the match and discarding everything else, in effect extracting your submatch. The trick is to make sure your regexp matches the entire string, with your desired pieces in parentheses.

Here's a examp.le of how you can pick out substrings...

                                    $1   $2   $3   $4   $5
rgxIdentifier.Pattern = "(.*)(!\S+)(\s+)(\S+)(.*)"
   
contentLine = "stuff before the exclamation mark...  !ID ABC more stuff matching to the end of the string"

identifier = rgxIdentifier.Replace(contentLine, "$2")    'Puts $2, i.e. !ID,  into identifier. Drops $1, $3 - 5 because you didn't use them in the replacement
symbol = rgxIdentifier.Replace(contentLine, "$4")      'Puts $4, i.e. ABC, into symbol. Drops $1-3, $5 because you didn't use them in the replacement

so afterwards

identifier = "!ID"
symbol = "ABC"

just as you'd want. :-)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

919 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

15 Experts available now in Live!

Get 1:1 Help Now