首先,不错的正则表达式学习基础
切入正题:
HTTP
响应output为
"<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n<root>\n <responseHeader>\n <sequence>1364044260890</sequence>\n <timestamp>2013-03-23 21:10:18</timestamp>\n <version>1.0.0</version>\n <returnCode>0</returnCode>\n <errorMessage>成功</errorMessage>\n </responseHeader>\n <responsePage>\n <hasNextPage>true</hasNextPage>\n <hasPrevIoUsPage>false</hasPrevIoUsPage>\n <lastPageNumber>4</lastPageNumber>\n <totalCount>62</totalCount>\n <thisPageFirstElementNumber>1</thisPageFirstElementNumber>\n <thisPageLastElementNumber>20</thisPageLastElementNumber>\n <nextPageNumber>2</nextPageNumber>\n <prevIoUsPageNumber>0</prevIoUsPageNumber>\n <pageSize>20</pageSize>\n <thisPageNumber>1</thisPageNumber>\n <firstResult>0</firstResult>\n </responsePage>\n <hotpointInfoList>\n <hotpointInfo>\n <name>天朗明居北区C N5栋</name>\n <nasid>301693</nasid>\n <address>广州市天河区中山大道与车陂路交汇处</address>\n <province>200</province>\n <cityCode>440100</cityCode>\n <type>99</type>\n <longitude>113.3862</longitude>\n <latitude>23.1296</latitude>\n <coverageArea>本次工程覆盖N5栋楼高26层。</coverageArea>\n <isRecommend>0</isRecommend>\n <distance>393.5081511793397</distance>\n </hotpointInfo>\n <hotpointInfo>\n <name>棠下街道办事处</name>\n <nasid>302082</nasid>\n <address>棠下棠德东路棠德小区E栋-1棠下街道办事处</address>\n <province>200</province>\n <cityCode>440100</cityCode>\n <type>99</type>\n <longitude>113.3815</longitude>\n <latitude>23.1354</latitude>\n <coverageArea>棠下街道办事处1-2F</coverageArea>\n <isRecommend>0</isRecommend>\n <distance>402.49152162918364</distance>\n </hotpointInfo>\n <hotpointInfo>\n <name>天朗明居南区C S1</name>\n <nasid>301690</nasid>\n <address>广州市天可区中山大道与车陂路交汇处</address>\n <province>200</province>\n <cityCode>440100</cityCode>\n <type>99</type>\n <longitude>113.3862</longitude>\n <latitude>23.1284</latitude>\n <coverageArea>S1栋</coverageArea>\n <isRecommend>0</isRecommend>\n <distance>499.4094297245712</distance>\n </hotpointInfo>\n <hotpointInfo>\n <name>正果工商所</name>\n <nasid>301470</nasid>\n <address>增城区正果大道88号</address>\n <province>200</province>\n <cityCode>440100</cityCode>\n <type>99</type>\n <longitude>113.3886</longitude>\n <latitude>23.1311</latitude>\n <coverageArea>工商所办公大厅、2F</coverageArea>\n <isRecommend>0</isRecommend>\n <distance>523.7109776951346</distance>\n </hotpointInfo>\n <hotpointInfo>\n <name>易初莲花车陂店C</name>\n <nasid>301864</nasid>\n <address>广州市天河区车陂路</address>\n <province>200</province>\n <cityCode>440100</cityCode>\n <type>99</type>\n <longitude>113.3854</longitude>\n <latitude>23.1258</latitude>\n <coverageArea>卖场区域</coverageArea>\n <isRecommend>0</isRecommend>\n <distance>748.1531798245414</distance>\n </hotpointInfo>\n <hotpointInfo>\n <name>骏景东御苑C8SW08</name>\n <nasid>301891</nasid>\n <address>广州市天河区中山大道190号</address>\n <province>200</province>\n <cityCode>440100</cityCode>\n <type>99</type>\n <longitude>113.3839</longitude>\n <latitude>23.1256</latitude>\n <coverageArea>G1-G6,H1-H2全部楼层公共区域</coverageArea>\n <isRecommend>0</isRecommend>\n <distance>749.2729513269494</distance>\n </hotpointInfo>\n <hotpointInfo>\n <name>骏景东御苑C7SW07</name>\n <nasid>301892</nasid>\n <address>广州市天河区中山大道190号</address>\n <province>200</province>\n <cityCode>440100</cityCode>\n <type>99</type>\n <longitude>113.3839</longitude>\n <latitude>23.1256</latitude>\n <coverageArea>G1-G6,H1-H2全部楼层公共区域</coverageArea>\n <isRecommend>0</isRecommend>\n <distance>749.2729513269494</distance>\n </hotpointInfo>\n <hotpointInfo>\n <name>骏景东御苑C4SW04</name>\n <nasid>301894</nasid>\n <address>广州市天河区中山大道190号</address>\n <province>200</province>\n <cityCode>440100</cityCode>\n <type>99</type>\n <longitude>113.3839</longitude>\n <latitude>23.1255</latitude>\n <coverageArea>G1-G6,H1-H2全部楼层公共区域</coverageArea>\n <isRecommend>0</isRecommend>\n <distance>760.1215323422311</distance>\n </hotpointInfo>\n <hotpointInfo>\n <name>骏景东御苑C3SW03</name>\n <nasid>301895</nasid>\n <address>广州市天河区中山大道190号</address>\n <province>200</province>\n <cityCode>440100</cityCode>\n <type>99</type>\n <longitude>113.3839</longitude>\n <latitude>23.1255</latitude>\n <coverageArea>G1-G6,H1-H2全部楼层公共区域</coverageArea>\n <isRecommend>0</isRecommend>\n <distance>760.1215323422311</distance>\n </hotpointInfo>\n <hotpointInfo>\n <name>骏景东御苑C2SW02</name>\n <nasid>301896</nasid>\n <address>广州市天河区中山大道190号</address>\n <province>200</province>\n <cityCode>440100</cityCode>\n <type>99</type>\n <longitude>113.3839</longitude>\n <latitude>23.1255</latitude>\n <coverageArea>G1-G6,H1-H2全部楼层公共区域</coverageArea>\n <isRecommend>0</isRecommend>\n <distance>760.1215323422311</distance>\n </hotpointInfo>\n <hotpointInfo>\n <name>骏景东御苑C5SW05</name>\n <nasid>301893</nasid>\n <address>广州市天河区中山大道190号</address>\n <province>200</province>\n <cityCode>440100</cityCode>\n <type>99</type>\n <longitude>113.3839</longitude>\n <latitude>23.1256</latitude>\n <coverageArea>G1-G6,H1-H2全部楼层公共区域</coverageArea>\n <isRecommend>0</isRecommend>\n <distance>760.1215323422311</distance>\n </hotpointInfo>\n <hotpointInfo>\n <name>骏景东御苑C6SW06</name>\n <nasid>301890</nasid>\n <address>广州市天河区中山大道190号</address>\n <province>200</province>\n <cityCode>440100</cityCode>\n <type>99</type>\n <longitude>113.3839</longitude>\n <latitude>23.1256</latitude>\n <coverageArea>G1-G6,H1-H2全部楼层公共区域</coverageArea>\n <isRecommend>0</isRecommend>\n <distance>760.3864516706291</distance>\n </hotpointInfo>\n <hotpointInfo>\n <name>骏景东御苑G3栋</name>\n <nasid>301898</nasid>\n <address>广州市天河区中山大道192号</address>\n <province>200</province>\n <cityCode>440100</cityCode>\n <type>99</type>\n <longitude>113.3838</longitude>\n <latitude>23.1254</latitude>\n <coverageArea>1F-28F</coverageArea>\n <isRecommend>0</isRecommend>\n <distance>771.2390231563601</distance>\n </hotpointInfo>\n <hotpointInfo>\n <name>南洋冠盛酒店</name>\n <nasid>302093</nasid>\n <address>天府路11号</address>\n <province>200</province>\n <cityCode>440100</cityCode>\n <type>99</type>\n <longitude>113.3805</longitude>\n <latitude>23.1258</latitude>\n <coverageArea>酒店1-13F</coverageArea>\n <isRecommend>0</isRecommend>\n <distance>805.295261820077</distance>\n </hotpointInfo>\n <hotpointInfo>\n <name>骏景花园南苑东区B骏灏轩D栋SW10</name>\n <nasid>301910</nasid>\n <address>广州市天河区中山大道旁骏景花园</address>\n <province>200</province>\n <cityCode>440100</cityCode>\n <type>99</type>\n <longitude>113.3819</longitude>\n <latitude>23.1239</latitude>\n <coverageArea>骏灏轩D栋</coverageArea>\n <isRecommend>0</isRecommend>\n <distance>967.8317796098889</distance>\n </hotpointInfo>\n <hotpointInfo>\n <name>骏景花园南苑东区B骏盈轩D栋SW15</name>\n <nasid>302068</nasid>\n <address>广州市天河区中山大道旁骏景花园</address>\n <province>200</province>\n <cityCode>440100</cityCode>\n <type>99</type>\n <longitude>113.3817</longitude>\n <latitude>23.1239</latitude>\n <coverageArea>骏盈轩D栋</coverageArea>\n <isRecommend>0</isRecommend>\n <distance>969.8902627929679</distance>\n </hotpointInfo>\n <hotpointInfo>\n <name>骏景花园南苑东区B骏盈轩B栋SW16</name>\n <nasid>302067</nasid>\n <address>广州市天河区中山大道旁骏景花园</address>\n <province>200</province>\n <cityCode>440100</cityCode>\n <type>99</type>\n <longitude>113.3818</longitude>\n <latitude>23.1239</latitude>\n <coverageArea>骏盈轩B栋</coverageArea>\n <isRecommend>0</isRecommend>\n <distance>969.8902627929679</distance>\n </hotpointInfo>\n <hotpointInfo>\n <name>骏景花园南苑东区B骏盈轩E栋SW14</name>\n <nasid>302069</nasid>\n <address>广州市天河区中山大道旁骏景花园</address>\n <province>200</province>\n <cityCode>440100</cityCode>\n <type>99</type>\n <longitude>113.3817</longitude>\n <latitude>23.1238</latitude>\n <coverageArea>骏盈轩E栋</coverageArea>\n <isRecommend>0</isRecommend>\n <distance>969.8902627929679</distance>\n </hotpointInfo>\n <hotpointInfo>\n <name>骏景花园南苑东区B骏灏轩F栋SW12</name>\n <nasid>302071</nasid>\n <address>广州市天河区中山大道旁骏景花园</address>\n <province>200</province>\n <cityCode>440100</cityCode>\n <type>99</type>\n <longitude>113.3817</longitude>\n <latitude>23.1238</latitude>\n <coverageArea>骏灏轩F栋</coverageArea>\n <isRecommend>0</isRecommend>\n <distance>969.8902627929679</distance>\n </hotpointInfo>\n <hotpointInfo>\n <name>骏景花园南苑东区B骏灏轩E栋SW11</name>\n <nasid>302072</nasid>\n <address>广州市天河区中山大道旁骏景花园</address>\n <province>200</province>\n <cityCode>440100</cityCode>\n <type>99</type>\n <longitude>113.3817</longitude>\n <latitude>23.1238</latitude>\n <coverageArea>骏灏轩E栋</coverageArea>\n <isRecommend>0</isRecommend>\n <distance>969.8902627929679</distance>\n </hotpointInfo>\n </hotpointInfoList>\n</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); }