Results 1 to 11 of 11

Thread: [RESOLVED] Regex help!

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2008
    Posts
    262

    Resolved [RESOLVED] Regex help!

    Hi Every1,

    I am using the below regular expression to grab the Article title out of this html source:

    Code:
    <p class=NoSpacing><span style='font-size:10.0pt;font-family:"Times New Roman"'>&lt;b&gt;ARTICLE
    TITLE: &lt;/b&gt; This is a Test title<o:p></o:p></span></p>
    
    <p class=NoSpacing><span style='font-size:10.0pt;font-family:"Times New Roman"'>&lt;b&gt;SELLING:
    &lt;/b&gt; Market Leading Products<o:p></o:p></span></p>
    
    <p class=NoSpacing><span style='font-size:10.0pt;font-family:"Times New Roman"'>&lt;b&gt;SELLING
    TO: &lt;/b&gt; High Profile Clients<o:p></o:p></span></p>
    Regular Expression:

    Code:
    (?<=TITLE: &lt;/b&gt;).+(?=<o:p></o:p></span></p>)
    The problem i have is when it grabs the information it is not stopping when it reaches <o></o></span></p>, insted it is capturing everything until the last instance of <o></o></span></p>.

    How do i rectify this?

    Kind regards,
    Chloe ~X~

  2. #2
    Addicted Member
    Join Date
    Jun 2005
    Posts
    169

    Re: Regex help!

    Check out somewhere like www.regexlib.com (also /cheatsheet.aspx)
    There is a handy tester there and there are also a fair few examples.

    I'm far from an expert in Regex but I think you should be looking for the text pattern you want rather than finding the start and end of the pattern. This is what I use to pick out repeating strings from other text

    Code:
    Dim rx as New Regex("pattern")
    For Each m As Match In rx.Matches("the text to check")
        DoSomething = m.Value
    Next

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

    Re: Regex help!

    try this:

    vb Code:
    1. Dim rx As New Regex("(?<=TITLE: &lt;/b&gt; ).+?(?=<o:p></o:p></span></p>)")
    2. MsgBox(rx.Match(testStr).Value)

    heres a regex tutorial + a regex reference site

  4. #4
    Hyperactive Member
    Join Date
    Apr 2009
    Posts
    358

    Re: Regex help!

    Quote Originally Posted by .paul. View Post
    try this:

    vb Code:
    1. Dim rx As New Regex("(?<=TITLE: &lt;/b&gt; ).+?(?=<o:p></o:p></span></p>)")
    2. MsgBox(rx.Match(testStr).Value)

    heres a regex tutorial + a regex reference site
    Hey, Paul, I got that to work, and I've been trying to figure out RegEx for some time, do you think you could kinda explain how each part works? I want to know how to match something in between two things. I can't figure it out. Please help.
    Sorry if my posts are misleading or sometimes rude, I'm just trying to get information so try to help me out.

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

    Re: Regex help!

    (?<=pattern) = find before specified string (next part)
    .+ = at least 1 char (any char = .) (at least 1 char = +)
    ? = specifies find smallest possible string that matches
    (?=pattern) = find after specified string (middle part)

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

    Re: Regex help!

    Download the free Expresso regex tool and play with it.
    http://www.ultrapico.com/Expresso.htm
    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
    Hyperactive Member
    Join Date
    Apr 2009
    Posts
    358

    Re: Regex help!

    Quote Originally Posted by stanav View Post
    Download the free Expresso regex tool and play with it.
    http://www.ultrapico.com/Expresso.htm
    I decided to make one of my own really quick, so if the match I want to find is
    "bunny" in "The purple bunny went hopping through the forest" what would my regular expression be?

    Edit: also, I want to pretend I don't know the word is bunny.
    Sorry if my posts are misleading or sometimes rude, I'm just trying to get information so try to help me out.

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

    Re: Regex help!

    Dim rx As New Regex("(?<=The purple ).+?(?= went hopping through the forest)")

  9. #9
    Hyperactive Member
    Join Date
    Apr 2009
    Posts
    358

    Re: Regex help!

    Quote Originally Posted by .paul. View Post
    Dim rx As New Regex("(?<=The purple ).+?(?= went hopping through the forest)")
    Thanks, that helps a lot.
    The only thing I need to know now is how to put all matches in a listbox. Can you tell me how to do that?
    Sorry if my posts are misleading or sometimes rude, I'm just trying to get information so try to help me out.

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

    Re: Regex help!

    vb Code:
    1. Dim rx as New Regex("pattern")
    2. For Each m As Match In rx.Matches("the text to check")
    3.     listbox1.items.add(m.Value)
    4. Next

  11. #11
    Hyperactive Member
    Join Date
    Apr 2009
    Posts
    358

    Re: Regex help!

    Quote Originally Posted by .paul. View Post
    vb Code:
    1. Dim rx as New Regex("pattern")
    2. For Each m As Match In rx.Matches("the text to check")
    3.     listbox1.items.add(m.Value)
    4. Next
    Thanks for helping me with RegEx .paul.
    rep+
    Sorry if my posts are misleading or sometimes rude, I'm just trying to get information so try to help me out.

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