実際に正則表現を使用してホットスポットデータを切り取り、ホットスポット単位のディレクトリーにカプセル化し、最後にリストを構成する.
16133 ワード
まず、正しい正規表現の学習基礎
正規表現30分入門チュートリアル
本題に入る:
HTTP
Outputは
hotpointInfoの中のaddress、name、distanceなどの情報
正規表現を使う
コードは以下の通りです
正規表現30分入門チュートリアル
本題に入る:
HTTP
Outputは
"<?xml version=\"1.0\" encoding=\"UTF-8\" ?>
<root>
<responseHeader>
<sequence>1364044260890</sequence>
<timestamp>2013-03-23 21:10:18</timestamp>
<version>1.0.0</version>
<returnCode>0</returnCode>
<errorMessage> </errorMessage>
</responseHeader>
<responsePage>
<hasNextPage>true</hasNextPage>
<hasPreviousPage>false</hasPreviousPage>
<lastPageNumber>4</lastPageNumber>
<totalCount>62</totalCount>
<thisPageFirstElementNumber>1</thisPageFirstElementNumber>
<thisPageLastElementNumber>20</thisPageLastElementNumber>
<nextPageNumber>2</nextPageNumber>
<previousPageNumber>0</previousPageNumber>
<pageSize>20</pageSize>
<thisPageNumber>1</thisPageNumber>
<firstResult>0</firstResult>
</responsePage>
<hotpointInfoList>
<hotpointInfo>
<name> C N5 </name>
<nasid>301693</nasid>
<address> </address>
<province>200</province>
<cityCode>440100</cityCode>
<type>99</type>
<longitude>113.3862</longitude>
<latitude>23.1296</latitude>
<coverageArea> N5 26 。</coverageArea>
<isRecommend>0</isRecommend>
<distance>393.5081511793397</distance>
</hotpointInfo>
<hotpointInfo>
<name> </name>
<nasid>302082</nasid>
<address> E -1 </address>
<province>200</province>
<cityCode>440100</cityCode>
<type>99</type>
<longitude>113.3815</longitude>
<latitude>23.1354</latitude>
<coverageArea> 1-2F</coverageArea>
<isRecommend>0</isRecommend>
<distance>402.49152162918364</distance>
</hotpointInfo>
<hotpointInfo>
<name> C S1</name>
<nasid>301690</nasid>
<address> </address>
<province>200</province>
<cityCode>440100</cityCode>
<type>99</type>
<longitude>113.3862</longitude>
<latitude>23.1284</latitude>
<coverageArea>S1 </coverageArea>
<isRecommend>0</isRecommend>
<distance>499.4094297245712</distance>
</hotpointInfo>
<hotpointInfo>
<name> </name>
<nasid>301470</nasid>
<address> 88 </address>
<province>200</province>
<cityCode>440100</cityCode>
<type>99</type>
<longitude>113.3886</longitude>
<latitude>23.1311</latitude>
<coverageArea> 、2F</coverageArea>
<isRecommend>0</isRecommend>
<distance>523.7109776951346</distance>
</hotpointInfo>
<hotpointInfo>
<name> C</name>
<nasid>301864</nasid>
<address> </address>
<province>200</province>
<cityCode>440100</cityCode>
<type>99</type>
<longitude>113.3854</longitude>
<latitude>23.1258</latitude>
<coverageArea> </coverageArea>
<isRecommend>0</isRecommend>
<distance>748.1531798245414</distance>
</hotpointInfo>
<hotpointInfo>
<name> C8SW08</name>
<nasid>301891</nasid>
<address> 190 </address>
<province>200</province>
<cityCode>440100</cityCode>
<type>99</type>
<longitude>113.3839</longitude>
<latitude>23.1256</latitude>
<coverageArea>G1-G6,H1-H2 </coverageArea>
<isRecommend>0</isRecommend>
<distance>749.2729513269494</distance>
</hotpointInfo>
<hotpointInfo>
<name> C7SW07</name>
<nasid>301892</nasid>
<address> 190 </address>
<province>200</province>
<cityCode>440100</cityCode>
<type>99</type>
<longitude>113.3839</longitude>
<latitude>23.1256</latitude>
<coverageArea>G1-G6,H1-H2 </coverageArea>
<isRecommend>0</isRecommend>
<distance>749.2729513269494</distance>
</hotpointInfo>
<hotpointInfo>
<name> C4SW04</name>
<nasid>301894</nasid>
<address> 190 </address>
<province>200</province>
<cityCode>440100</cityCode>
<type>99</type>
<longitude>113.3839</longitude>
<latitude>23.1255</latitude>
<coverageArea>G1-G6,H1-H2 </coverageArea>
<isRecommend>0</isRecommend>
<distance>760.1215323422311</distance>
</hotpointInfo>
<hotpointInfo>
<name> C3SW03</name>
<nasid>301895</nasid>
<address> 190 </address>
<province>200</province>
<cityCode>440100</cityCode>
<type>99</type>
<longitude>113.3839</longitude>
<latitude>23.1255</latitude>
<coverageArea>G1-G6,H1-H2 </coverageArea>
<isRecommend>0</isRecommend>
<distance>760.1215323422311</distance>
</hotpointInfo>
<hotpointInfo>
<name> C2SW02</name>
<nasid>301896</nasid>
<address> 190 </address>
<province>200</province>
<cityCode>440100</cityCode>
<type>99</type>
<longitude>113.3839</longitude>
<latitude>23.1255</latitude>
<coverageArea>G1-G6,H1-H2 </coverageArea>
<isRecommend>0</isRecommend>
<distance>760.1215323422311</distance>
</hotpointInfo>
<hotpointInfo>
<name> C5SW05</name>
<nasid>301893</nasid>
<address> 190 </address>
<province>200</province>
<cityCode>440100</cityCode>
<type>99</type>
<longitude>113.3839</longitude>
<latitude>23.1256</latitude>
<coverageArea>G1-G6,H1-H2 </coverageArea>
<isRecommend>0</isRecommend>
<distance>760.1215323422311</distance>
</hotpointInfo>
<hotpointInfo>
<name> C6SW06</name>
<nasid>301890</nasid>
<address> 190 </address>
<province>200</province>
<cityCode>440100</cityCode>
<type>99</type>
<longitude>113.3839</longitude>
<latitude>23.1256</latitude>
<coverageArea>G1-G6,H1-H2 </coverageArea>
<isRecommend>0</isRecommend>
<distance>760.3864516706291</distance>
</hotpointInfo>
<hotpointInfo>
<name> G3 </name>
<nasid>301898</nasid>
<address> 192 </address>
<province>200</province>
<cityCode>440100</cityCode>
<type>99</type>
<longitude>113.3838</longitude>
<latitude>23.1254</latitude>
<coverageArea>1F-28F</coverageArea>
<isRecommend>0</isRecommend>
<distance>771.2390231563601</distance>
</hotpointInfo>
<hotpointInfo>
<name> </name>
<nasid>302093</nasid>
<address> 11 </address>
<province>200</province>
<cityCode>440100</cityCode>
<type>99</type>
<longitude>113.3805</longitude>
<latitude>23.1258</latitude>
<coverageArea> 1-13F</coverageArea>
<isRecommend>0</isRecommend>
<distance>805.295261820077</distance>
</hotpointInfo>
<hotpointInfo>
<name> B D SW10</name>
<nasid>301910</nasid>
<address> </address>
<province>200</province>
<cityCode>440100</cityCode>
<type>99</type>
<longitude>113.3819</longitude>
<latitude>23.1239</latitude>
<coverageArea> D </coverageArea>
<isRecommend>0</isRecommend>
<distance>967.8317796098889</distance>
</hotpointInfo>
<hotpointInfo>
<name> B D SW15</name>
<nasid>302068</nasid>
<address> </address>
<province>200</province>
<cityCode>440100</cityCode>
<type>99</type>
<longitude>113.3817</longitude>
<latitude>23.1239</latitude>
<coverageArea> D </coverageArea>
<isRecommend>0</isRecommend>
<distance>969.8902627929679</distance>
</hotpointInfo>
<hotpointInfo>
<name> B B SW16</name>
<nasid>302067</nasid>
<address> </address>
<province>200</province>
<cityCode>440100</cityCode>
<type>99</type>
<longitude>113.3818</longitude>
<latitude>23.1239</latitude>
<coverageArea> B </coverageArea>
<isRecommend>0</isRecommend>
<distance>969.8902627929679</distance>
</hotpointInfo>
<hotpointInfo>
<name> B E SW14</name>
<nasid>302069</nasid>
<address> </address>
<province>200</province>
<cityCode>440100</cityCode>
<type>99</type>
<longitude>113.3817</longitude>
<latitude>23.1238</latitude>
<coverageArea> E </coverageArea>
<isRecommend>0</isRecommend>
<distance>969.8902627929679</distance>
</hotpointInfo>
<hotpointInfo>
<name> B F SW12</name>
<nasid>302071</nasid>
<address> </address>
<province>200</province>
<cityCode>440100</cityCode>
<type>99</type>
<longitude>113.3817</longitude>
<latitude>23.1238</latitude>
<coverageArea> F </coverageArea>
<isRecommend>0</isRecommend>
<distance>969.8902627929679</distance>
</hotpointInfo>
<hotpointInfo>
<name> B E SW11</name>
<nasid>302072</nasid>
<address> </address>
<province>200</province>
<cityCode>440100</cityCode>
<type>99</type>
<longitude>113.3817</longitude>
<latitude>23.1238</latitude>
<coverageArea> E </coverageArea>
<isRecommend>0</isRecommend>
<distance>969.8902627929679</distance>
</hotpointInfo>
</hotpointInfoList>
</root>"
切り取りが必要ですhotpointInfoの中のaddress、name、distanceなどの情報
正規表現を使う
コードは以下の通りです
MatchCollection hotspotMatches = Regex.Matches(output, "<hotpointInfo>.*?<name>(?<name>.*?)</name>.*?<address>(?<address>.*?)</address>.*?<cityCode>(?<city>.*?)</cityCode>.*?<type>(?<hottype>.*?)</type>.*?<longitude>(?<longitude>.*?)</longitude>.*?<latitude>(?<latitude>.*?)</latitude>.*?(.*?<coverageArea>(?<coverarea>.*?)</coverageArea>)?.*?<distance>(?<distance>.*?)</distance>.*?</hotpointInfo>", RegexOptions.Singleline);
foreach (Match hotspotMatch in hotspotMatches)
{
Dictionary<String, String> dict = new Dictionary<string, string>();
double distanceDou = double.Parse(hotspotMatch.Groups["distance"].Value);
//make display 110 meter
//not 110.12232453242 meter
int distanceInt = (int)distanceDou;
dict.Add("name", hotspotMatch.Groups["name"].Value);
dict.Add("address", hotspotMatch.Groups["address"].Value);
string city = FromCityIdToName(hotspotMatch.Groups["city"].Value);
dict.Add("city", city);
dict.Add("hottype", hotspotMatch.Groups["hottype"].Value);
dict.Add("longitude", hotspotMatch.Groups["longitude"].Value);
dict.Add("latitude", hotspotMatch.Groups["latitude"].Value);
dict.Add("coverarea", hotspotMatch.Groups["coverarea"].Value);
dict.Add("distance", Convert.ToString(distanceInt));
dict.Add("total", totalPage);
((List<Dictionary<String, String>>)returnData).Add(dict);
}