RegEx: Using Variables in Regular Expressions

First off, I'd like to thank Steve Smith for helping me out yesterday with a major brain <insert your favorite phrase here>. Like always, my issue turned out to be something stupid. Anyway, my issue ended up bringing something else to my attention. Why not use regular expressions to grab data from strings being passed in and then do something with that data. Hmmm.

It took me a little bit to figure out the logistics of what I wanted to accomplish. But from a Regex end, it was quite simple. Basically, I wanted to use variables in a regular expression to parse through data. I have a string that says: Hello, my name is Jason and his name is Bob. I'd like to first obtain the match to ensure that I receive the "name is %" string. Then I'd like to obtain each name in a variable so I can run some process against it. This is shown in the following sample:

    Function ObtainResults() As String
        Dim 
rtn As String = ""
        
Dim str As String = "Hello, my name is Jason and his name is Bob."
        
        
Dim obj As New Regex("name is(?<name>.*?)")
        
        
Dim result As Match
        
        
For Each result In obj.Matches(str)
            
'The statement below grabs each |name| and adds it the the rtn string
            
rtn rtn & "Result: " & result.Groups("name").Value & "<br/>"
        
Next
        
        Return 
rtn
    
End Function

As you can see, it's fairly simple to establish and has some better uses than this. :-)

Published Wednesday, January 31, 2007 11:00 AM by Jason N. Gaylord
Filed under: ,

Comments

# re: RegEx: Using Variables in Regular Expressions

Wednesday, January 31, 2007 1:28 PM by Sean

The terminology "variable" is not correct. I think it is called "group" name. As you collect groups with your regular expression.

And yes, it is a very handy feature when you need to slice and dice your input.  

# re: RegEx: Using Variables in Regular Expressions

Thursday, February 01, 2007 12:30 PM by Jason N. Gaylord

Yes, it is a group. But when using it this way, you can use it like a variable "value".

# re: RegEx: Using Variables in Regular Expressions

Sunday, February 18, 2007 12:49 PM by mark

I don't think ypur regex is correct. The problem is *.? means any character, any repetitions, as few as possible.  As few possible means it would match any.  Also your missing a space.

Here a simple regex that works.

name is (?<name>[a-zA-Z]+)

# re: RegEx: Using Variables in Regular Expressions

Friday, November 28, 2008 3:51 AM by Olgunka-jo

<a href= aseeds.one.angelfire.com >transvestite rockstar</a>

# re: RegEx: Using Variables in Regular Expressions

Friday, November 28, 2008 10:58 AM by Olgunka-xo

<a href= http://fasster.angelfire.com >baltimore and convention center and headquarters</a> <a href= http://gertui.angelfire.com >nasdaq 100 tennis tournament</a>

# re: RegEx: Using Variables in Regular Expressions

Friday, November 28, 2008 4:35 PM by Olgunka-sw

<a href= http://fairra.angelfire.com >landls end</a> <a href= http://vonucshka.angelfire.com >chancellor internal med</a>

# re: RegEx: Using Variables in Regular Expressions

Friday, November 28, 2008 9:41 PM by Olgunka-ut

<a href= http://chkola.angelfire.com >avlastkey</a> <a href= http://bustersw.angelfire.com >how to start a strawberry patch in alabama</a>

# re: RegEx: Using Variables in Regular Expressions

Saturday, November 29, 2008 3:05 AM by Olgunka-kz

<a href= http://kustur.angelfire.com >dad vail regatta</a> <a href= http://trututa.angelfire.com >ratings apartments eagle ridge alabama</a>

# re: RegEx: Using Variables in Regular Expressions

Wednesday, December 03, 2008 12:25 AM by Asina

<a href= bestpre.com ></a>

# re: RegEx: Using Variables in Regular Expressions

Saturday, December 06, 2008 8:28 AM by Semil

<a href= spiritez.com ></a>

# re: RegEx: Using Variables in Regular Expressions

Saturday, February 28, 2009 10:03 PM by fabiola-dm

<a href= adultpersonalsfinder.com >find partner</a>

# re: RegEx: Using Variables in Regular Expressions

Saturday, February 28, 2009 10:07 PM by balabo3_aq

<a href= adultpersonalsfinder.com >find partner</a>

Leave a Comment

(required) 
(required) 
(optional)
(required)