Regular Expressions

  • 15 years ago

    Here is a topic I have never seen here, and seems to be a devoid area of development.


    I am working with text triggers, and I need to beable to get variables out of the text that matches a certain pattern.


    such patterns like: "hello, my name is *. I am # years old. I have * colored hair." or whatever with control characters.


    I have been trying to use the Microsoft Regular Expressions Reference 5.5.  It will match variables with the regex key characters, but I can't get the variables, out of the string, such as if the string is "Hello, my name is omnilord.  I am 21 years old.  I have brown colored hair."


    I want to get Omnilord, 21, and brown out of the match string, but all I get out of it is the entire string!  Another trip is that in this case I have been trying multiple word names, and I want the whole name, not one word names.  Here is my code thus far



    Code:

    Option Explicit


    Private Sub Form_Load()
    Dim TestStr(3) As String, a As Long
    Dim re As New RegExp, m As Match


    TestStr(0) = "blah blah blah"
    TestStr(1) = "Hello I am Eric Van Student.  I am 34 years old.  Who are you?"
    TestStr(2) = "Hello, my name is Peppay.  I am 80 years old.  Who are you?"
    TestStr(3) = "Hello, my name is Eric Van Stubent.  I am 40 years old.  Who are you?"


    re.Pattern = "Hello, my name is \w+.  I am \d{1,3} years old.  Who are you?"
    re.Global = False
    re.IgnoreCase = True


    For a = 0 To 3
     For Each m In re.Execute(TestStr(a))
        MsgBox m.Value
     Next
    Next a


    End
    End Sub

Post a reply

No one has replied yet! Why not be the first?

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“In order to understand recursion, one must first understand recursion.”