Odd Regexp Issue

K

Kyle Heck

I'm writing a web crawler, and in that crawler I want to remove all
scripts in the pages I crawl.

I should be able to do a simple gsub!(/<!--.*-->/,"") right? Well, I do
that and unfortunately it doesn't remove some scripts. Take google for
instance. It removes the first script, but not the second. I'm really
confused. Since google has two scripts, <!-- happens twice, so do -->
so it's not like the full regexp should ever fail to be triggered.

Any insight on the issue would be GREAT?! :D

Thanks,
Kyle Heck
 
L

Luis Parravicini

I should be able to do a simple gsub!(/<!--.*-->/,"") right? Well, I do
that and unfortunately it doesn't remove some scripts. Take google for
instance. It removes the first script, but not the second. I'm really
confused. Since google has two scripts, <!-- happens twice, so do -->
so it's not like the full regexp should ever fail to be triggered.

gsub(/<!--.*?-->/m,"")

If there are new lines inside the string you need to use the m
modifier to make the dot (.) include new lines as well.
And the ? is to make the match non-greedy. Without it it would match
the start of the first script and the end of the last script.
 
J

Joel VanderWerf

Kyle said:
I'm writing a web crawler, and in that crawler I want to remove all
scripts in the pages I crawl.

I should be able to do a simple gsub!(/<!--.*-->/,"") right? Well, I do
that and unfortunately it doesn't remove some scripts. Take google for
instance. It removes the first script, but not the second. I'm really
confused. Since google has two scripts, <!-- happens twice, so do -->
so it's not like the full regexp should ever fail to be triggered.

Any insight on the issue would be GREAT?! :D

Thanks,
Kyle Heck

Try multiline mode: gsub!(/<!--.*-->/m,"")
 
K

Kyle Heck

Well, that seemed to do the trick :D

Thanks a lot, I didn't know that regexp only applied to one line by
default, HRM!

Thanks,
Kyle Heck
 
R

Rob Biedenharn

gsub(/<!--.*?-->/m,"")

If there are new lines inside the string you need to use the m
modifier to make the dot (.) include new lines as well.
And the ? is to make the match non-greedy. Without it it would match
the start of the first script and the end of the last script.

Of course, you do realize that you're saying "scripts" but you're
removing "comments" with this regexp.

I can have:
<script type="text/javascript">
//<![CDATA[
<%= yield :page_scripts %>
//]]>
</script>

in a page with not a <!-- or --> in sight!

-Rob

Rob Biedenharn http://agileconsultingllc.com
(e-mail address removed)
 
R

Rob Biedenharn

Well, that seemed to do the trick :D

Thanks a lot, I didn't know that regexp only applied to one line by
default, HRM!

Thanks,
Kyle Heck

Actually, it is the . expression that doesn't match a newline without
the 'm' option. That option just changes '.' from matching "any
character except a newline" to matching "any character". The Regular
Expression section of chapter 22 in the pickaxe covers all this (p.
324-328)

-Rob

Rob Biedenharn http://agileconsultingllc.com
(e-mail address removed)
 
J

Jenda Krynicky

Kyle said:
I'm writing a web crawler, and in that crawler I want to remove all
scripts in the pages I crawl.

I should be able to do a simple gsub!(/<!--.*-->/,"") right? Well, I do
that and unfortunately it doesn't remove some scripts. Take google for
instance. It removes the first script, but not the second. I'm really
confused. Since google has two scripts, <!-- happens twice, so do -->
so it's not like the full regexp should ever fail to be triggered.

Any insight on the issue would be GREAT?! :D

Thanks,
Kyle Heck

I'm not sure what are you after actually, but apart from the <script>
tags Rob mentioned, you might need to remove the onClick, onMouseOver
and other handlers. And since the handlers can be within almost any tag
it would be very hard to find and remove them correctly with just a few
regexps. You should use a real HTML parser (the preffered Ruby one seems
to be called hpricot ... I guess the author wanted to be funny). If this
is meant to make the display of the pages secure you should also rather
"keep only the tags and attributes that are safe" than "remove stuff
that's not safe". You might easily overlook something.

If you happened to use the-language-that-musn't-be-named, you'd just use
HTML::TagFilter
(http://search.cpan.org/~wross/HTML-TagFilter-1.03/TagFilter.pm). Good
luck.

Jenda
 

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

Similar Threads


Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,906
Latest member
SkinfixSkintag

Latest Threads

Top