# Trying to use regex

**URL:** <https://www.ruby-forum.com/t/trying-to-use-regex/107304>\
**Category:** Ruby\
**Created:** [June 20, 2007, 10:15am UTC](https://www.ruby-forum.com/t/trying-to-use-regex/107304 "2007-06-20T10:15:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![merrittr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e480ec/32.png) [@merrittr](https://www.ruby-forum.com/u/merrittr)\
**Post date:** [June 20, 2007, 10:15am UTC](https://www.ruby-forum.com/t/trying-to-use-regex/107304/1 "2007-06-20T10:15:46Z")

</div>

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[^\>]\*\>(.+?)/)[0]  
puts text

---

<div class="post-metadata">

**Author:** ![merrittr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e480ec/32.png) [@merrittr](https://www.ruby-forum.com/u/merrittr)\
**Post date:** [June 20, 2007, 10:42am UTC](https://www.ruby-forum.com/t/trying-to-use-regex/107304/2 "2007-06-20T10:42:51Z")

</div>

On 20 Jun 2007, at 17:15, merrittr wrote:

> #! /usr/bin/ruby
> 
> @h = File.open “test.html”  
> @response = @h.gets
> 
> text = @response.scan(/\<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 G.

Bioinformatics Center  
Kyoto University

---

<div class="post-metadata">

**Author:** ![merrittr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e480ec/32.png) [@merrittr](https://www.ruby-forum.com/u/merrittr)\
**Post date:** [June 20, 2007, 4:37pm UTC](https://www.ruby-forum.com/t/trying-to-use-regex/107304/3 "2007-06-20T16:37:52Z")

</div>

On Jun 20, 2007, at 4:41 AM, Alex G. wrote:

> > #! /usr/bin/ruby  
> > text = @response.scan(/\<body[^\>]\*\>(.+?)\</body\>/m)[0]
> 
> Alex G.
> 
> Bioinformatics Center  
> Kyoto University

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

text = @response.scan(%r{\<body\b._?\>(._?)}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 B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![merrittr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e480ec/32.png) [@merrittr](https://www.ruby-forum.com/u/merrittr)\
**Post date:** [June 20, 2007, 4:42pm UTC](https://www.ruby-forum.com/t/trying-to-use-regex/107304/4 "2007-06-20T16:42:51Z")

</div>

merrittr wrote:

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

HTML parsing can get quite complicated, why not use a library? I’ve  
heard great things about [http://code.whytheluckystiff.net/hpricot/](http://code.whytheluckystiff.net/hpricot/)
