Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > how to extract something in between a pattern

Reply
Thread Tools

how to extract something in between a pattern

 
 
Cheyne Li
Guest
Posts: n/a
 
      09-22-2008
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-forum.com/.

 
Reply With Quote
 
 
 
 
Li Chen
Guest
Posts: n/a
 
      09-22-2008
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:oc "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-forum.com/.

 
Reply With Quote
 
 
 
 
Tod Beardsley
Guest
Posts: n/a
 
      09-22-2008
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 <> 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:oc "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-forum.com/.
>
>




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

 
Reply With Quote
 
suroot57@gmail.com
Guest
Posts: n/a
 
      09-25-2008
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-forum.com/.


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

(a.split %r{</?p>})[1]
 
Reply With Quote
 
Ittay Dror
Guest
Posts: n/a
 
      09-25-2008
[Note: parts of this message were removed to make it a legal post.]



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-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<p>ccddee</p>'
=> "aabbcc<p>ccddee</p>"
irb(main):002:0> a[%r{<p>(.*)</p>}, 1]
=> "ccddee"



>


--
Ittay Dror <>
Tikal <http://www.tikalk.com>
Tikal Project <http://tikal.sourceforge.net>


--
--
Ittay Dror <>


 
Reply With Quote
 
Patrick He
Guest
Posts: n/a
 
      09-28-2008
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"
>


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
How do i extract vidios when winrar wont extract them??? help plzzzzzzzz smuttdog@sc.rr.com Computer Support 2 12-23-2007 07:03 AM
howto extract type pattern from xsd shema file / or DOM Document object from JAVA andrzej.gdula@gmail.com Java 0 10-11-2007 09:18 AM
boolean endsWith(String s, Pattern pattern) lepikhin@gmail.com Java 17 11-16-2005 10:31 AM
Re: extract the bitmap pattern of font file. Martijn C Programming 0 07-28-2003 10:25 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57