# Return first line of parsing

**URL:** <https://www.ruby-forum.com/t/return-first-line-of-parsing/112329>\
**Category:** Ruby\
**Created:** [August 17, 2007, 3:52pm UTC](https://www.ruby-forum.com/t/return-first-line-of-parsing/112329 "2007-08-17T15:52:47Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![h4z3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/54ee81/32.png) [@h4z3](https://www.ruby-forum.com/u/h4z3)\
**Post date:** [August 17, 2007, 3:52pm UTC](https://www.ruby-forum.com/t/return-first-line-of-parsing/112329/1 "2007-08-17T15:52:47Z")

</div>

mysite.each {|line|  
if line =~ /

**(.+)\</b\>/  
puts “#{$2} found at: #{$1}”  
end  
}**

Ok guys, Lets say the website has 50+ lines… and i only want to return  
the first one, any ideas?

---

<div class="post-metadata">

**Author:** ![h4z3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/54ee81/32.png) [@h4z3](https://www.ruby-forum.com/u/h4z3)\
**Post date:** [August 17, 2007, 4:02pm UTC](https://www.ruby-forum.com/t/return-first-line-of-parsing/112329/2 "2007-08-17T16:02:55Z")

</div>

On 8/17/07, Haze N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> mysite.each {|line|  
> if line =~ /
> 
> **(.+)\</b\>/  
> puts “#{$2} found at: #{$1}”  
> end  
> }**
> 
>  
> 
> Ok guys, Lets say the website has 50+ lines… and i only want to return  
> the first one, any ideas?

 

%r/^(.\*)$/.match(mysite)[1]

---

<div class="post-metadata">

**Author:** ![h4z3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/54ee81/32.png) [@h4z3](https://www.ruby-forum.com/u/h4z3)\
**Post date:** [August 18, 2007, 12:02am UTC](https://www.ruby-forum.com/t/return-first-line-of-parsing/112329/3 "2007-08-18T00:02:50Z")

</div>

On Aug 17, 2007, at 8:59 AM, Tim P. wrote:

> %r/^(.\*)$/.match(mysite)[1]

Careful,  
What if the site’s white space has been stripped? (no CR or LF at all)  
or if the html/xhtml is screwy? (old html without closed elements,  
or just poorly formed or badly nested)

---

<div class="post-metadata">

**Author:** ![h4z3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/54ee81/32.png) [@h4z3](https://www.ruby-forum.com/u/h4z3)\
**Post date:** [August 18, 2007, 1:08am UTC](https://www.ruby-forum.com/t/return-first-line-of-parsing/112329/4 "2007-08-18T01:08:57Z")

</div>

On Friday 17 August 2007 02:59:39 pm John J. wrote:

> > > Ok guys, Lets say the website has 50+ lines… and i only want to  
> > > return  
> > > the first one, any ideas?
> > 
> > %r/^(.\*)$/.match(mysite)[1]
> 
> Careful,  
> What if the site’s white space has been stripped? (no CR or LF at all)  
> or if the html/xhtml is screwy? (old html without closed elements,  
> or just poorly formed or badly nested)

The first line of any file doesn’t depend on the html in it. His code  
should return the first line of any file – html or no.

---

<div class="post-metadata">

**Author:** ![h4z3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/54ee81/32.png) [@h4z3](https://www.ruby-forum.com/u/h4z3)\
**Post date:** [August 18, 2007, 1:28am UTC](https://www.ruby-forum.com/t/return-first-line-of-parsing/112329/5 "2007-08-18T01:28:58Z")

</div>

On Aug 17, 8:52 am, Haze N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Posted viahttp://www.ruby-forum.com/.  
> If you want to use essentially the same block as above, but just take  
> the first matching line:

mysite.each {|line|  
if line =~ /

**(.+)\</b\>/  
puts “#{$2} found at: #{$1}”  
break  
end  
}**

 

Tim’s solution would give you the first line of the actual html file  
and, as John mentions, that could be the entire web page if there are  
no CR/LF characters in the file.

 

Jeremy
