Specific Regular Expression (PREG) Help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • FiberOptix
    New Member
    • Jun 2010
    • 5

    #1

    Specific Regular Expression (PREG) Help

    Hey all,

    My first foray in to regular expressions has not been easy, with the Perl-style regular expressions that preg_match requires complicating things slightly for me, as I find that many of the email or website seeking regex I find simply won't work... Probably for some small reason attributable to the different style, that I'm unable to discern.

    The code that I'm trying to write is looking for a specific email address, website, and also a name within html source, which looks like the following:

    <br> E-mail: <a href="mailto:em ail@address.com ">email@address .com</a><br>
    <br> Web site: <a href="http://www.address.com ">www.address.c om</a><br>

    <td>
    Vice President <br /> Joe Smith <br>
    </td>

    Wanting to collect the e-mail, url, and name, can someone help with conjuring up a regular expression I can plug in to preg_match?

    Thanks very much
  • dlite922
    Recognized Expert Top Contributor
    • Dec 2007
    • 1586

    #2
    regex for email and URL you can easily find on the web, however for the name it's going to be a little more difficult to find.

    If every html source you're going to look through is that exact format (i.e. it will always say Vice President space <br/> Name name </br>

    Then yes, you can write a regexp for that. Just that literal string is enough:

    Code:
    preg_match('/Vice President <br />.+<br>/',$line)
    With regex there's no one correct answer, the answer depends on your use and _exactly_ what you want. You might think you were pretty thorough in your post, but you can get much more detailed than that.

    For example you can write a regex to compare the mailto address to make sure it's exactly the same as content of the link. I.e you can make it not match the following:

    <a href="mailto:so meaddress@domai n.foo">somethin gelse</a>

    Cheers,


    Dan

    Comment

    • FiberOptix
      New Member
      • Jun 2010
      • 5

      #3
      Originally posted by dlite922
      regex for email and URL you can easily find on the web, however for the name it's going to be a little more difficult to find.

      If every html source you're going to look through is that exact format (i.e. it will always say Vice President space <br/> Name name </br>

      Then yes, you can write a regexp for that. Just that literal string is enough:

      Code:
      preg_match('/Vice President <br />.+<br>/',$line)
      With regex there's no one correct answer, the answer depends on your use and _exactly_ what you want. You might think you were pretty thorough in your post, but you can get much more detailed than that.

      For example you can write a regex to compare the mailto address to make sure it's exactly the same as content of the link. I.e you can make it not match the following:

      <a href="mailto:so meaddress@domai n.foo">somethin gelse</a>

      Cheers,


      Dan
      Thanks very much for your helpful reply Dan.

      I had a rough idea of how the regex would work, but the regex you post is extremely helpful as I was trying to find the correct syntax for 'capture everything between,' which is part of your regular expression. Just one syntactical issue with your preg... It seems to grab both the name and the html tags also... Is it possible to only get the name?

      Thanks very much!

      Comment

      • FiberOptix
        New Member
        • Jun 2010
        • 5

        #4
        Originally posted by dlite922
        regex for email and URL you can easily find on the web, however for the name it's going to be a little more difficult to find.

        If every html source you're going to look through is that exact format (i.e. it will always say Vice President space <br/> Name name </br>

        Then yes, you can write a regexp for that. Just that literal string is enough:

        Code:
        preg_match('/Vice President <br />.+<br>/',$line)
        With regex there's no one correct answer, the answer depends on your use and _exactly_ what you want. You might think you were pretty thorough in your post, but you can get much more detailed than that.

        For example you can write a regex to compare the mailto address to make sure it's exactly the same as content of the link. I.e you can make it not match the following:

        <a href="mailto:so meaddress@domai n.foo">somethin gelse</a>

        Cheers,


        Dan
        I just figured it out! By using (.+), I can use the handy feature of preg_match, that successive indices after the zeroth one (the full match) reference the parenthesized matches.

        Comment

        Working...