Removing data between two HTML tags?

Discussion in 'Perl Misc' started by Rob, Apr 8, 2011.

  1. Rob

    Rob Guest

    Hello,

    I am trying to use perl to remove all data which comes between these
    two tags:

    <div class="left">......</div>


    This is the code I have so far but it is not doing the job:

    $description=~ s|(.*? <div \s+ class="left"> .*? <div \s+
    class="left"> .*?)<div \s+ class="left"> .*? </div>(.*? <div \s+
    class="left">)|$1$2|sxg;

    Any help would be appreciated.

    Many thanks

    Rob
    Rob, Apr 8, 2011
    #1
    1. Advertising

  2. Rob

    James Wright Guest

    On 04/08/11 00:41, Rob wrote:
    > Hello,
    >
    > I am trying to use perl to remove all data which comes between these
    > two tags:
    >
    > <div class="left">......</div>
    >
    >
    > This is the code I have so far but it is not doing the job:
    >
    > $description=~ s|(.*?<div \s+ class="left"> .*?<div \s+
    > class="left"> .*?)<div \s+ class="left"> .*?</div>(.*?<div \s+
    > class="left">)|$1$2|sxg;
    >
    > Any help would be appreciated.
    >
    > Many thanks
    >
    > Rob


    Use an HTML parser.
    James Wright, Apr 8, 2011
    #2
    1. Advertising

  3. Rob

    Marc Girod Guest

    Marc Girod, Apr 8, 2011
    #3
  4. Rob

    Marc Girod Guest

    Marc Girod, Apr 8, 2011
    #4
  5. Rob

    ccc31807 Guest

    On Apr 8, 6:45 am, Henry Law <> wrote:
    > My bitter experience tells me that only pain lies along this road you
    > are taking.


    Still, it's instructive to make the attempt. A long time ago I
    attempted to do something like this by building an RE incrementally,
    piece by piece. I failed, but the lessons I learned have stood me in
    very good stead, and several times a month I build complex REs the
    same way. And guess what? I don't fail!

    I'm not saying that the OP should reinvent the wheel, but that the OP
    needs to go through the process of reinventing the wheel so he can
    understand how the original wheel inventors did it, and then he can
    use that knowledge to invent something useful on his own.

    CC.
    ccc31807, Apr 8, 2011
    #5
  6. Rob

    brian d foy Guest

    In article
    <>,
    Rob <> wrote:

    > Hello,
    >
    > I am trying to use perl to remove all data which comes between these
    > two tags:
    >
    > <div class="left">......</div>


    My article "Process HTML with a Perl Module" may get you started in the
    right direction:

    http://www.informit.com/articles/article.aspx?p=1581075
    brian d foy, Apr 8, 2011
    #6
  7. Rob

    Rob Guest

    On Apr 8, 5:03 pm, brian d foy <> wrote:
    > In article
    > <>,


    Thank you all for your help and suggestions. I have achieved what I
    wanted by just creating HTML comments around the text to remove and
    removing that instead, as follows:

    $data =~ s/<!-- start-top -->.+?<!-- end-top -->//smg;

    The article at http://tinyurl.com/64uvzgp set me on the right track.

    Rob
    Rob, Apr 8, 2011
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    937
    Stefan Behnel
    Jul 28, 2007
  2. Francesco Moi
    Replies:
    2
    Views:
    232
    Gunnar Hjalmarsson
    May 31, 2004
  3. Maqo
    Replies:
    4
    Views:
    137
    A. Sinan Unur
    Feb 23, 2005
  4. replacing tags between tags

    , Sep 18, 2005, in forum: Perl Misc
    Replies:
    9
    Views:
    123
    Jürgen Exner
    Sep 19, 2005
  5. Jeroen

    Removing html tags

    Jeroen, Aug 25, 2009, in forum: Javascript
    Replies:
    2
    Views:
    96
    Thomas 'PointedEars' Lahn
    Aug 25, 2009
Loading...

Share This Page