How to extract something in between a pattern

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

ccddee

I wanna get the anything between

and

, which is ccddee

Thanks in advance.

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

ccddee

I wanna get the anything between

and

, which is ccddee

Thanks in advance.

C:\Users\Alex>irb
irb(main):001:0> require ‘hpricot’
=> true
irb(main):002:0> a=“aabbcc

ccddee


=> “aabbcc

ccddee


irb(main):003:0>
irb(main):004:0* doc=Hpricot(a)
=> #<Hpricot::Doc “aabbcc” {elem

“ccddee”

}>
irb(main):005:0> p doc.at(‘p’).inner_text
“ccddee”
=> nil
irb(main):006:0>

Li

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

Otherwise, standard regex stuff would apply, imo.

On Sep 22, 12:58 pm, Cheyne Li [email protected] wrote:


Posted viahttp://www.ruby-forum.com/.

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

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

[email protected] wrote:

Thanks in advance.

Posted viahttp://www.ruby-forum.com/.

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

ccddee


=> “aabbcc

ccddee


irb(main):002:0> a[%r{

(.*)

}, 1]
=> “ccddee”


Ittay D. [email protected]
Tikal http://www.tikalk.com
Tikal Project http://tikal.sourceforge.net

Or:

irb(main):004:0> a = ‘aabbcc

ccddee

ccc

eee


=> “aabbcc

ccddee

ccc

eee


irb(main):005:0> a.scan(%r{

([^<]*)

})
=> [[“ccddee”], [“eee”]]

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