trying to use regex

M

merrittr

hi i am trying to strip out text between body tags but when run it i
get:

rob@rob-laptop:~/ruby$ ./html2.rb
../html2.rb:14: unknown regexp options - bdy
../html2.rb:14: unterminated string meets end of file
../html2.rb:14: parse error, unexpected tSTRING_END, expecting
tSTRING_CONTENT or tREGEXP_END or tSTRING_DBEG or tSTRING_DVAR




#! /usr/bin/ruby

@h = File.open "test.html"
@response = @h.gets

text = @response.scan(/<body[^>]*>(.+?)</body>/)[0]
puts text
 
A

Alex Gutteridge

hi i am trying to strip out text between body tags but when run it i
get:

rob@rob-laptop:~/ruby$ ./html2.rb
./html2.rb:14: unknown regexp options - bdy
./html2.rb:14: unterminated string meets end of file
./html2.rb:14: parse error, unexpected tSTRING_END, expecting
tSTRING_CONTENT or tREGEXP_END or tSTRING_DBEG or tSTRING_DVAR




#! /usr/bin/ruby

@h = File.open "test.html"
@response = @h.gets

text = @response.scan(/<body[^>]*>(.+?)</body>/)[0]
puts text

You need to escape the '/' in your regexp, and unless your html file
is one line you may need to also add the multiline option:

text = @response.scan(/<body[^>]*>(.+?)<\/body>/m)[0]

Alex Gutteridge

Bioinformatics Center
Kyoto University
 
R

Rob Biedenharn

hi i am trying to strip out text between body tags but when run it i
get:

rob@rob-laptop:~/ruby$ ./html2.rb
./html2.rb:14: unknown regexp options - bdy
./html2.rb:14: unterminated string meets end of file
./html2.rb:14: parse error, unexpected tSTRING_END, expecting
tSTRING_CONTENT or tREGEXP_END or tSTRING_DBEG or tSTRING_DVAR

#! /usr/bin/ruby

@h = File.open "test.html"
@response = @h.gets

text = @response.scan(/<body[^>]*>(.+?)</body>/)[0]
puts text

You need to escape the '/' in your regexp, and unless your html
file is one line you may need to also add the multiline option:

text = @response.scan(/<body[^>]*>(.+?)<\/body>/m)[0]

Alex Gutteridge

Bioinformatics Center
Kyoto University

Or you can use the %r{} form of a Regexp literal:

text = @response.scan(%r{<body\b.*?>(.*?)</body>}mi)[0]

\b matches a "word boundary"
m is the multi-line option that causes . to match newlines, too
i is the case insensitive option (so BODY would also be matched)

-Rob

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

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,768
Messages
2,569,575
Members
45,054
Latest member
LucyCarper

Latest Threads

Top