//为什么匹配不出来?对正则不太熟,想问下原因,以及如何修改
       //sHtmlText的内容 :<CODE>2005</CODE><DES>listing product to trademe error:You can't have more than 10 custom shipping options.\r\n</DES>\r\n
        public static string[] GetHtmlImageUrlList(string sHtmlText)
        {
            string pattern = "<CODE>(?<code>[^<>]+)</CODE>.*<DES>(?<des>.*)</DES>";
            //string pattern1 = "<DES>(?<des>.*)</DES>";
            Regex regImg = new Regex(pattern, RegexOptions.IgnoreCase);
            MatchCollection matches = regImg.Matches(sHtmlText);    //搜索匹配的字符串 
            string[] result = new string[2];
            foreach (Match match in matches)
            {
                result[0] = match.Groups["code"].Value.ToString();
                result[1] = match.Groups["des"].Value.ToString();
            }
            return result;
        }
正则RegExc#

解决方案 »

  1.   

    经过断点分布测试,我发现 在des中有 "\r\n" 照成无法匹配出数据。单独有 "\r" 也是可以匹配出来的 那这个要如何修改 让 "\r\n" 也能匹配出来呢
      

  2.   

    <CODE>(?<code>[^<>]+)</CODE>.*<DES>(?<des>[\s\S]*?)</DES>
      

  3.   

    public static string[] GetHtmlImageUrlList(string sHtmlText)
    {
    string pattern = "(?is)<CODE>(?<code>.*?)</CODE>.*?<DES>(?<des>.*?)</DES>";
     
    Regex regImg = new Regex(pattern, RegexOptions.IgnoreCase);
    MatchCollection matches = regImg.Matches(sHtmlText);    
    string[] result = new string[2];
    foreach (Match match in matches)
    {
    result[0] = match.Groups["code"].Value.ToString();
    result[1] = match.Groups["des"].Value.ToString();
    }
    return result;
    }