[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: Newbie questions

Dav Jones

12/12/2006 3:32:00 PM

Thanks. I've got it reading in a file and checking each line against a
regular expression.

How would I go about taking individual sections of a line?
i.e. if the word 'example:' appears on a line then it takes that and a
few characters before or after it (it will be something specific its
just the question is in general here) so for
blah blah blah example: twenty blah blah
it returns me 'example: twenty'

--
Posted via http://www.ruby-....

2 Answers

Paul Lutus

12/12/2006 5:57:00 PM

0

Dav Jones wrote:

> Thanks. I've got it reading in a file and checking each line against a
> regular expression.
>
> How would I go about taking individual sections of a line?
> i.e. if the word 'example:' appears on a line then it takes that and a
> few characters before or after it (it will be something specific its
> just the question is in general here) so for
> blah blah blah example: twenty blah blah
> it returns me 'example: twenty'

----------------------------------

#!/usr/bin/ruby -w

data = "blah blah blah blah example: twenty blah blah blah blah"

result = data.scan(%r{(example: \w+)}).flatten

puts result

----------------------------------

output:

example: twenty

--
Paul Lutus
http://www.ara...

dblack

12/12/2006 6:07:00 PM

0