?
Solved

parenthesis in regular expressions

Posted on 2003-12-10
5
Medium Priority
?
558 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

Independent Software Vendors: 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

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…
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…
Suggested Courses

777 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