Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

parenthesis in regular expressions

Posted on 2003-12-10
5
Medium Priority
?
559 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
[X]
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
5 Comments
 
LVL 1

Accepted Solution

by:
JSheldon earned 500 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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
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…
Suggested Courses

598 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