日期:2014-05-17  浏览次数:20441 次

正则表达式设计问题
XML code
<?xml version="1.0"?><xml_api_reply version="1"><weather module_id="0" tab_id="0" mobile_row="0" mobile_zipped="1" row="0" section="0" ><forecast_information><city data="Beijing, Beijing"/><postal_code data="Beijing"/><latitude_e6 data=""/><longitude_e6 data=""/><forecast_date data="2012-04-21"/><current_date_time data="2012-04-21 19:00:00 +0000"/><unit_system data="SI"/></forecast_information><current_conditions><condition data="阴"/><temp_f data="57"/><temp_c data="14"/><humidity data="湿度: 82%"/><icon data="/ig/images/weather/cn_overcast.gif"/><wind_condition data="风向: 北、风速:1 米/秒"/></current_conditions><forecast_conditions><day_of_week data="周六"/><low data="7"/><high data="19"/><icon data="/ig/images/weather/mostly_sunny.gif"/><condition data="以晴为主"/></forecast_conditions><forecast_conditions><day_of_week data="周日"/><low data="12"/><high data="25"/><icon data="/ig/images/weather/mostly_sunny.gif"/><condition data="以晴为主"/></forecast_conditions><forecast_conditions><day_of_week data="周一"/><low data="16"/><high data="29"/><icon data="/ig/images/weather/sunny.gif"/><condition data="晴"/></forecast_conditions><forecast_conditions><day_of_week data="周二"/><low data="10"/><high data="20"/><icon data="/ig/images/weather/chance_of_rain.gif"/><condition data="可能有雨"/></forecast_conditions></weather></xml_api_reply>

正则表达式怎样设计能读取data=""中的数据。就是只要data=""中的数据。
有谁会啊

------解决方案--------------------
LZ的需求应该不是单一的正则式可以处理的,正则式处理的大前提是针对固定格式的字符串进行按规则的字符过滤处理,如果各字段的data内容规格不一致的话,很难进行统一的正则式的过滤,如果“湿度:15%”中汉字应过滤掉,但在data="有可能有雨"中,汉字是正确内容,不能过滤。所以如果一定需要通过正则式过滤,可以考虑针对单一节点的data内容进行正则式的编写,通过XML配置文件进行统一管理。
------解决方案--------------------
(?is)(?<=data\=\")[^\"]*?(?=\")
------解决方案--------------------
XML配置文件可以如下处理:

<XML>
<Node Name="city" Regex="[\s+,]+">
<Node Name="postal_code" Regex="[\s+,]+">
......
</XML>
------解决方案--------------------
对Dom操作:
C# code
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(Server.MapPath("~/test.xml"));
        XmlNodeList nodes = xmlDoc.SelectNodes(@"//forecast_conditions");
        foreach (XmlNode node in nodes)
        {
            Response.Write(node["low"].Attributes["data"].Value + "<br/>");
            Response.Write(node["high"].Attributes["data"].Value + "<br/>");
            Response.Write(node["condition"].Attributes["data"].Value + "<br/>");
            Response.Write("<br/><br/>");
        }