Solved

parenthesis in regular expressions

Posted on 2003-12-10
5
550 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

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…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

708 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

14 Experts available now in Live!

Get 1:1 Help Now