Solved

parenthesis in regular expressions

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

Technology Partners: 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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VBA filters 2 71
passing a value with stream reader AFTER a ";" 3 83
How to compare ms sql hashbytes results within vb6 5 97
Set email body to html using vbscript 6 71
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

733 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