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

J

John

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)
 
S

Sherm Pendley

John said:
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--
 
G

Gunnar Hjalmarsson

John said:
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;
 
T

Tad McClellan

John said:
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)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top