Re: データ抽出クラス(正規表現で取得する場合)
以下の使用サンプル。
データ抽出クラス - A More Beautiful day
シンボル :regexp に正規表現を使って、値を取得する。
以下のようなデータがあるとすると
<root> <title>extract test</title> <record> <item> <name>user01</name> <id>0</id> <date>2007/11/25</date> <comment>comment01 foobar</comment> </item> <item> <name>user02</name> <id>1</id> <date>2007/11/28</date> <comment>comment02 foobar</comment> </item> <item> <name>user03</name> <id>2</id> <date>2007/11/30</date> <comment>comment03 foobar</comment> </item> </record> </root>
以下のような ruby ソースコードで
data = Extract.match :text => text do summary :title => { :regexp => '<title>(.+?)</title>' } record :regexp => '<item>(.+?)</item>' do item :name => { :regexp => '<name>(.+?)</name>' } item :id => { :regexp => '<id>(.+?)</id>' } item :date => { :regexp => '<date>(.+?)</date>' } item :comment => { :regexp => '<comment>(.+?)</comment>' } end end
以下のようなハッシュデータを取得できる
{ :summary=>{:title=>"extract test"}, :item=> [{:name=>"user01", :date=>"2007/11/25", :id=>"0", :comment=>"comment01 foobar"}, {:name=>"user02", :date=>"2007/11/28", :id=>"1", :comment=>"comment02 foobar"}, {:name=>"user03", :date=>"2007/11/30", :id=>"2", :comment=>"comment03 foobar"}] }