关于 "正则" 的小问题。求解答

寂小魔 2013-08-08 02:48:25

//为什么匹配不出来?对正则不太熟,想问下原因,以及如何修改
//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;
}
...全文
85 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
寂小魔 2013-08-08
  • 打赏
  • 举报
回复
Thanks
q107770540 2013-08-08
  • 打赏
  • 举报
回复
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;
		}
shawn_yang 2013-08-08
  • 打赏
  • 举报
回复
<CODE>(?<code>[^<>]+)</CODE>.*<DES>(?<des>[\s\S]*?)</DES>
寂小魔 2013-08-08
  • 打赏
  • 举报
回复
经过断点分布测试,我发现 在des中有 "\r\n" 照成无法匹配出数据。单独有 "\r" 也是可以匹配出来的 那这个要如何修改 让 "\r\n" 也能匹配出来呢
寂小魔 2013-08-08
  • 打赏
  • 举报
回复
引用 1 楼 shawn_yang 的回复:
怎么诡异?为什么我的 都不进循环?不是正则的问题?为什么其他有能进?
shawn_yang 2013-08-08
  • 打赏
  • 举报
回复

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧