parenthesis in regular expressions

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

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


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)

Question by:josephfluckiger
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

Accepted Solution

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:

For a good reference of Visual Basic Regular expressions pattern property check:

Author Comment

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.

Expert Comment

ID: 9918245
Quite wordy indeed, and IMO not as powerful, but it gets the job done :)

Expert Comment

ID: 9920081

Expert Comment

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. :-)

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
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…
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…

707 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