On Mon, Oct 1, 2012 at 11:32 PM, ajay paswan <[email protected]> wrote: > How to sparse a string like: > > suppose input is str="<a>1</a><a>22</a><a>3</a>" > I want str_a=["<a>1</a>","<a>22</a>","<a>3</a>"] > How can I get str_a from str?
I'd use a proper XML or HTML processing tool for that. Then you can search with XPath '//a'. $ irb19 -r nokogiri irb(main):001:0> str = "<a>1</a><a>22</a><a>3</a>" => "<a>1</a><a>22</a><a>3</a>" irb(main):004:0> dom = Nokogiri.HTML str => #<Nokogiri::HTML::Document:0x439c00c name="document" children=[#<Nokogiri::XML::DTD:0x439bdd2 name="html">, #<Nokogiri::XML::Element:0x439b940 name="html" children=[#<Nokogiri::XML::Element:0x439b80a name="body" children=[#<Nokogiri::XML::Element:0x439b6b6 name="a" children=[#<Nokogiri::XML::Text:0x439b53a "1">]>, #<Nokogiri::XML::Element:0x439b3be name="a" children=[#<Nokogiri::XML::Text:0x439b27e "22">]>, #<Nokogiri::XML::Element:0x439b152 name="a" children=[#<Nokogiri::XML::Text:0x439b030 "3">]>]>]>]> irb(main):005:0> str_a = dom.xpath '//a' => [#<Nokogiri::XML::Element:0x439b6b6 name="a" children=[#<Nokogiri::XML::Text:0x439b53a "1">]>, #<Nokogiri::XML::Element:0x439b3be name="a" children=[#<Nokogiri::XML::Text:0x439b27e "22">]>, #<Nokogiri::XML::Element:0x439b152 name="a" children=[#<Nokogiri::XML::Text:0x439b030 "3">]>] irb(main):006:0> str_a.size => 3 Kind regards robert5 -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/ -- You received this message because you are subscribed to the Google Groups ruby-talk-google group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at https://groups.google.com/d/forum/ruby-talk-google?hl=en
