[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

how to extract something in between a pattern

Cheyne Li

9/22/2008 4:58:00 PM

Hi experts,

I'm not very familiar with ruby's library. I wonder if there a method
can extract something in a pattern? For example,

I have a string: a=aabbcc<p>ccddee</p>

I wanna get the anything between <p> and </p>, which is ccddee


Thanks in advance.
--
Posted via http://www.ruby-....

5 Answers

Li Chen

9/22/2008 5:45:00 PM

0

Cheyne Li wrote:
> Hi experts,
>
> I'm not very familiar with ruby's library. I wonder if there a method
> can extract something in a pattern? For example,
>
> I have a string: a=aabbcc<p>ccddee</p>
>
> I wanna get the anything between <p> and </p>, which is ccddee
>
>
> Thanks in advance.


C:\Users\Alex>irb
irb(main):001:0> require 'hpricot'
=> true
irb(main):002:0> a="aabbcc<p>ccddee</p>"
=> "aabbcc<p>ccddee</p>"
irb(main):003:0>
irb(main):004:0* doc=Hpricot(a)
=> #<Hpricot::Doc "aabbcc" {elem <p> "ccddee" </p>}>
irb(main):005:0> p doc.at('p').inner_text
"ccddee"
=> nil
irb(main):006:0>



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

Tod Beardsley

9/22/2008 6:11:00 PM

0

Ya, in this case (HTML/XML), Hpricot is your best bet.

Otherwise, standard regex stuff would apply, imo.

On Mon, Sep 22, 2008 at 12:44 PM, Li Chen <chen_li3@yahoo.com> wrote:
> Cheyne Li wrote:
>> Hi experts,
>>
>> I'm not very familiar with ruby's library. I wonder if there a method
>> can extract something in a pattern? For example,
>>
>> I have a string: a=aabbcc<p>ccddee</p>
>>
>> I wanna get the anything between <p> and </p>, which is ccddee
>>
>>
>> Thanks in advance.
>
>
> C:\Users\Alex>irb
> irb(main):001:0> require 'hpricot'
> => true
> irb(main):002:0> a="aabbcc<p>ccddee</p>"
> => "aabbcc<p>ccddee</p>"
> irb(main):003:0>
> irb(main):004:0* doc=Hpricot(a)
> => #<Hpricot::Doc "aabbcc" {elem <p> "ccddee" </p>}>
> irb(main):005:0> p doc.at('p').inner_text
> "ccddee"
> => nil
> irb(main):006:0>
>
>
>
> Li
> --
> Posted via http://www.ruby-....
>
>



--
todb@planb-security.net | ICQ: 335082155 | Note: Due to Google's
privacy policy <http://tinyurl.com... and the United States'
policy on electronic surveillance <http://tinyurl.com...,
please do not IM/e-mail me anything you wish to remain secret.

suroot57

9/25/2008 12:21:00 AM

0

On Sep 22, 12:58 pm, Cheyne Li <happy.go.lucky....@gmail.com> wrote:
> Hi experts,
>
> I'm not very familiar with ruby's library. I wonder if there a method
> can extract something in a pattern? For example,
>
> I have a string: a=aabbcc<p>ccddee</p>
>
> I wanna get the anything between <p> and </p>, which is ccddee
>
> Thanks in advance.
> --
> Posted viahttp://www.ruby-....

If you want to use regexp, a quick and dirty way would be :

(a.split %r{</?p>})[1]

Ittay Dror

9/25/2008 5:58:00 AM

0

[Note: parts of this message were removed to make it a legal post.]



suroot57@gmail.com wrote:
> On Sep 22, 12:58 pm, Cheyne Li <happy.go.lucky....@gmail.com> wrote:
>
>> Hi experts,
>>
>> I'm not very familiar with ruby's library. I wonder if there a method
>> can extract something in a pattern? For example,
>>
>> I have a string: a=aabbcc<p>ccddee</p>
>>
>> I wanna get the anything between <p> and </p>, which is ccddee
>>
>> Thanks in advance.
>> --
>> Posted viahttp://www.ruby-....
>>
>
> If you want to use regexp, a quick and dirty way would be :
>
> (a.split %r{</?p>})[1]
>
or:
irb(main):001:0> a = 'aabbcc<p>ccddee</p>'
=> "aabbcc<p>ccddee</p>"
irb(main):002:0> a[%r{<p>(.*)</p>}, 1]
=> "ccddee"



>

--
Ittay Dror <ittayd@tikalk.com>
Tikal <http://www.tika...
Tikal Project <http://tikal.sourcefor...


--
--
Ittay Dror <ittay.dror@gmail.com>


Patrick He

9/28/2008 7:42:00 PM

0

Or:

irb(main):004:0> a = 'aabbcc<p>ccddee</p>ccc<p>eee</p>'
=> "aabbcc<p>ccddee</p>ccc<p>eee</p>"
irb(main):005:0> a.scan(%r{<p>([^<]*)</p>})
=> [["ccddee"], ["eee"]]

I perfer to specify what character(s) not to match explicitly.


Ittay Dror wrote:
>
> or:
> irb(main):001:0> a = 'aabbcc<p>ccddee</p>'
> => "aabbcc<p>ccddee</p>"
> irb(main):002:0> a[%r{<p>(.*)</p>}, 1]
> => "ccddee"
>