regular expression to extract text

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • suzanne.boyle@gmail.com

    #1

    regular expression to extract text

    Hi

    I have an html file with headings followed by one or more paragraphs
    like this

    <h2>blah blah 1</h2>
    <p>more blah blah blah</p>

    <h2>blah blah 2</h2>
    <p>more blah blah blah</p>
    <p>even more blah blah blah</p>

    I'd like to extract the text of the headings and the related
    paragraphs and insert them into a database. So far I've managed to
    get the heading text but cant figure out how to get the associated
    paragraphs. I've been using regular expressions, here is the
    expression I have so far <h2[.]*>(.+?)</h2>(.+?). This gets the text
    of the headings but not the paragraphs and now I'm basically stumped.

    Any help would be appreciated.
  • shimmyshack

    #2
    Re: regular expression to extract text

    On Nov 25, 9:48 pm, suzanne.bo...@g mail.com wrote:
    Hi
    >
    I have an html file with headings followed by one or more paragraphs
    like this
    >
    <h2>blah blah 1</h2>
    <p>more blah blah blah</p>
    >
    <h2>blah blah 2</h2>
    <p>more blah blah blah</p>
    <p>even more blah blah blah</p>
    >
    I'd like to extract the text of the headings and the related
    paragraphs and insert them into a database. So far I've managed to
    get the heading text but cant figure out how to get the associated
    paragraphs. I've been using regular expressions, here is the
    expression I have so far <h2[.]*>(.+?)</h2>(.+?). This gets the text
    of the headings but not the paragraphs and now I'm basically stumped.
    >
    Any help would be appreciated.
    you could do this another way, although reg exp is a great way.
    have you thought that you could use xml to so this.
    since you are obviosuly starting with something which is basically
    xml, why not just load the string as xml (topping and tailing it if
    needed) and then extract using xpath.

    Comment

    • Kailash Nadh

      #3
      Re: regular expression to extract text

      Slightly unorthodox, but this works.

      <?php

      preg_match_all( "/((<h2>(.+?)<\/h2>(.+?)<p>(.+? )<\/p>))/is", $html,
      $matches);
      print_r($matche s);

      // array[3] would be headings and array[5] would be the related
      paragraph text
      ?>

      Comment

      • suzanne.boyle@gmail.com

        #4
        Re: regular expression to extract text

        The problem with using xml is that the html is coming from Word so it
        contains a lot of unnecessary crap and isn't valid xml. And since I
        don't have much experience parsing xml in php I thought it would be
        easier to use regular expressions to extract the sections I want.

        And I'm almost there now, the expression Kailash wrote almost works
        but it only gives the first paragraph after the heading. I just need
        to work out how to extract the rest of the paragraphs.

        Comment

        Working...