Results 1 to 8 of 8

Thread: [RESOLVED] RegEx questions!

  1. #1

    Thread Starter
    Frenzied Member dinosaur_uk's Avatar
    Join Date
    Sep 2004
    Location
    Jurassic Park
    Posts
    1,098

    Resolved [RESOLVED] RegEx questions!

    Hi all,

    I am a RegEx virgin and have no idea where to start. I have three questions which i would be more than gratefull if you could help me.

    1. Where is the best place to start learning RegEx?
    2. If i have an array list full of text, how can i search for a string which is in the list?

    For example, if i enter the search string "bob test" in this text:
    Hi there, this is bob giving a testing

    it will return a true. However, if there is only one match, it will also return a true.

    3. thirdly, how can i search and pick out the link which is surrounded with the following tags? <link>http://etc etc te</link>

    CHeers,

    Ken
    If you find my thread helpful, please remember to rate me

  2. #2
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: RegEx questions!

    RegExLib.com is the best place to start and for implementing with .net you could visit MSDN
    Please mark you thread resolved using the Thread Tools as shown

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,420

    Re: RegEx questions!

    I can answer 1 of your questions

    1/ Try this

  4. #4
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: RegEx questions!

    1. I use this free tool to learn/test regex patterns
    http://www.ultrapico.com/Expresso.htm

    2. Use a List(Of String) is a better choice than the old arraylist class. You can then call one of its find methods (Find, FindAll, FindIndex, FindLast....). MSDN even has an example on this.

    3. Using regex.
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  5. #5

    Thread Starter
    Frenzied Member dinosaur_uk's Avatar
    Join Date
    Sep 2004
    Location
    Jurassic Park
    Posts
    1,098

    Re: RegEx questions!

    Ok,

    I've found that I can use the following regex to pick up the link, but how do i capture the text?

    (<link>) will capture the first one, but how do i know when it is ending?

    <[^<]+?> will capture all tags between which has < > enclosing it.

    Could you please help me? I dont know how to capture the link enclosed between <link></link>

    cheers
    If you find my thread helpful, please remember to rate me

  6. #6
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: RegEx questions!

    Try this pattern
    Code:
    Dim pattern as string = "(?<=\<link\>).+(?=\</link\>)"
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  7. #7

    Thread Starter
    Frenzied Member dinosaur_uk's Avatar
    Join Date
    Sep 2004
    Location
    Jurassic Park
    Posts
    1,098

    Re: RegEx questions!

    ' Regex match
    Dim options As RegexOptions = RegexOptions.None
    Dim regex As System.Text.RegularExpressions.Regex = New System.Text.RegularExpressions.Regex("<link\b[^>]*>(.*?)</link>")
    Dim input As String = Me.TextBox1.Text

    ' Get matches
    Dim matches As MatchCollection = regex.Matches(input)
    Dim i As Integer
    For i = 0 To matches.Count - 1
    ' TODO: Do something with result
    System.Windows.Forms.MessageBox.Show(matches(i).Groups(1).Value, "Match")
    Next i
    If you find my thread helpful, please remember to rate me

  8. #8

    Thread Starter
    Frenzied Member dinosaur_uk's Avatar
    Join Date
    Sep 2004
    Location
    Jurassic Park
    Posts
    1,098

    Re: RegEx questions!

    vb Code:
    1. ' Regex match
    2.         Dim options As RegexOptions = RegexOptions.None
    3.         Dim regex As System.Text.RegularExpressions.Regex = New System.Text.RegularExpressions.Regex("<link\b[^>]*>(.*?)</link>")
    4.         Dim input As String = Me.TextBox1.Text
    5.  
    6.         ' Get matches
    7.         Dim matches As MatchCollection = regex.Matches(input)
    8.         Dim i As Integer
    9.         For i = 0 To matches.Count - 1
    10.             ' TODO: Do something with result
    11.             System.Windows.Forms.MessageBox.Show(matches(i).Groups(1).Value, "Match")
    12.         Next i
    If you find my thread helpful, please remember to rate me

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width