Regex help, need to find all "<script src" calls but not if the call has menu.js in it.

Discussion in 'Perl Misc' started by John, Jan 7, 2005.

  1. John

    John Guest

    I have been reading but cannot find a way to find all strings that
    start with <script src but disreguard the ones that have menu.js as
    the call.

    Examples:

    <script src=script.js></script> (keep)
    <script src=/widjets/footer.js></script> (keep)
    <script src=/thingys/left_nav.js></script> (keep)
    <script src=menu.js></script> (throw away)
    John, Jan 7, 2005
    #1
    1. Advertising

  2. Re: Regex help, need to find all "<script src" calls but not if thecall has menu.js in it.

    John wrote:

    > I have been reading but cannot find a way to find all strings that
    > start with <script src but disreguard the ones that have menu.js as
    > the call.


    You're using the wrong tool for the job. Don't use regexes to parse
    HTML, use the module designed for that: HTML::parser.

    sherm--

    --
    Cocoa programming in Perl: http://camelbones.sourceforge.net
    Hire me! My resume: http://www.dot-app.org
    Sherm Pendley, Jan 7, 2005
    #2
    1. Advertising

  3. Re: Regex help, need to find all "<script src" calls but not if thecall has menu.js in it.

    John wrote:
    > I have been reading but cannot find a way to find all strings that
    > start with <script src but disreguard the ones that have menu.js as
    > the call.
    >
    > Examples:
    >
    > <script src=script.js></script> (keep)
    > <script src=/widjets/footer.js></script> (keep)
    > <script src=/thingys/left_nav.js></script> (keep)
    > <script src=menu.js></script> (throw away)


    grep !/menu\.js/, $string =~ /<script\s+src.+?<\/script\s*>/gis;

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Jan 7, 2005
    #3
  4. John <> wrote:
    > I have been reading but cannot find a way to find all strings that
    > start with <script src but disreguard the ones that have menu.js as
    > the call.



    "Parsing" HTML with regular expressions is only OK is some
    special situations. I'll assume you know for a fact that your
    particular situation is suitably special. (If you need it to
    work reliably, use a real parser for HTML.)


    Use a negative look-ahead:

    ---------------------------------
    #!/usr/bin/perl
    use warnings;
    use strict;

    while ( <DATA> ) {
    print if /<script src=(?!menu\.js)/;
    }


    __DATA__
    <script src=script.js></script> (keep)
    <script src=/widjets/footer.js></script> (keep)
    <script src=/thingys/left_nav.js></script> (keep)
    <script src=menu.js></script> (throw away)
    ---------------------------------


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Jan 8, 2005
    #4
    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:
    2
    Views:
    895
    Bengt Richter
    Aug 1, 2005
  2. Angus
    Replies:
    3
    Views:
    319
  3. Replies:
    3
    Views:
    730
    Reedick, Andrew
    Jul 1, 2008
  4. Blue®
    Replies:
    10
    Views:
    297
    Thomas 'PointedEars' Lahn
    May 27, 2005
  5. Bob
    Replies:
    5
    Views:
    249
Loading...

Share This Page