求一正则表达式(关于html取数据)

frank1314 2008-10-17 08:01:24
求一正则表达式
<p>你好</p>
结果是:你好
< >中为任意数字或字母或符号,谁知道?
...全文
116 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
frank1314 2008-10-17
  • 打赏
  • 举报
回复
谢了,一时没看见!
root_ 2008-10-17
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 frank1314 的回复:]
省了都不行,救命啊!!
[/Quote]

代码都已经给到这份上了,楼主再说不行那我也无话可说

最后再强调一下,楼主用的是 mc1[i].Groups["content"].Value
而我的代码取的是      mc1[i].Groups[2].Value;

再不行我也只能对楼主读代码能力和语言表达能力深表遗憾了
止戈而立 2008-10-17
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 root_ 的回复:]
贴你的测试代码,我测试没问题


C# codestring test = "<p> <font size=\"3\" color=\"#008000\">你好</font> </p> ";
MatchCollection mc = Regex.Matches(test, @"<([^\s>]*)[^>]*>([^<>]*)</\1>");
foreach (Match m in mc)
{
richTextBox1.Text += m.Groups[2].Value + "\n";
}
[/Quote]

这个是正解。。
frank1314 2008-10-17
  • 打赏
  • 举报
回复
省了都不行,救命啊!!
root_ 2008-10-17
  • 打赏
  • 举报
回复
不需要用的时候,不要滥用RegexOptions.IgnoreCase|RegexOptions.Compiled这些参数
root_ 2008-10-17
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 frank1314 的回复:]
打错

刚才的也是别人的,都不行!
[/Quote]

从哪里搞来的Groups["content"],能行才怪

MatchCollection本来就是一个集合,有必要再搞个数组吗,就算是一定要,用泛型也比数组来得容易

string test = "<p> <font size=\"3\" color=\"#008000\">你好</font> </p> ";
MatchCollection mc = Regex.Matches(test, @"<([^\s>]*)[^>]*>([^<>]*)</\1>");
List<string> list = new List<string>();
foreach (Match m in mc)
{
list.Add(m.Groups[2].Value);
}


算了,我还是按楼主的代码写个吧

string test = "<p> <font size=\"3\" color=\"#008000\">你好</font> </p> ";
MatchCollection mc1 = Regex.Matches(test, @"<([^\s>]*)[^>]*>([^<>]*)</\1>");
string[] div1 = new string[mc1.Count];
for (int i = 0; i < mc1.Count; i++)
{
div1[i] = mc1[i].Groups[2].Value;
}
frank1314 2008-10-17
  • 打赏
  • 举报
回复
打错
string str=" <p> <font size=\"3\" color=\"#008000\">你好 </font> </p> ";
Regex htmlRegex1 = new Regex(@"<([^\s>]*)[^>]*>([^<>]*)</\1>", RegexOptions.IgnoreCase | RegexOptions.Compiled);
MatchCollection mc1 = htmlRegex1.Matches(str); //抽取标题
string[] div1 = new string[mc1.Count];
for (int i = 0; i < mc1.Count; i++)
{
div1[i] = mc1[i].Groups["content"].Value;
}






刚才的也是别人的,都不行!
root_ 2008-10-17
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 frank1314 的回复:]
string str=" <p> <font size=\"3\" color=\"#008000\">你好 </font> </p> ";
Regex htmlRegex1 = new Regex(@" <.*>(.*)? </.*>", RegexOptions.IgnoreCase | RegexOptions.Compiled);
MatchCollection mc1 = htmlRegex1.Matches(str); //抽取标题
string[] div1 = new string[mc1.Count];
for (int i = 0; i < mc1.Count; i++)
{
div1[i] = mc1[i].Groups["content"].Value;
}

[/Quote]

我汗,敢情我这正则是白写了。。。
frank1314 2008-10-17
  • 打赏
  • 举报
回复
string str="<p> <font size=\"3\" color=\"#008000\">你好</font> </p> ";
Regex htmlRegex1 = new Regex(@"<.*>(.*)?</.*>", RegexOptions.IgnoreCase | RegexOptions.Compiled);
MatchCollection mc1 = htmlRegex1.Matches(str); //抽取标题
string[] div1 = new string[mc1.Count];
for (int i = 0; i < mc1.Count; i++)
{
div1[i] = mc1[i].Groups["content"].Value;
}
root_ 2008-10-17
  • 打赏
  • 举报
回复
贴你的测试代码,我测试没问题

string test = "<p> <font size=\"3\" color=\"#008000\">你好</font> </p> ";
MatchCollection mc = Regex.Matches(test, @"<([^\s>]*)[^>]*>([^<>]*)</\1>");
foreach (Match m in mc)
{
richTextBox1.Text += m.Groups[2].Value + "\n";
}
frank1314 2008-10-17
  • 打赏
  • 举报
回复
取不到,取到空值
<p><font size="3" color="#008000">你好</font></p>
得不到:你好
root_ 2008-10-17
  • 打赏
  • 举报
回复
试下是否符合要求

MatchCollection mc = Regex.Matches(str, @"<([^\s>]*)[^>]*>([^<>]*)</\1>");
foreach (Match m in mc)
{
richTextBox1.Text += m.Groups[2].Value + "\n";
}
frank1314 2008-10-17
  • 打赏
  • 举报
回复
不好意思,没说清楚,是提取,
例如:
<p><font>你好</font></p>
出来结果是:你好
root_ 2008-10-17
  • 打赏
  • 举报
回复
需求不明确,你这算是提取还是替换呢,给个复杂些的例子,并给出结果

string result = Regex.Replace(yourStr, @"<[^>]*>", "");
MessageBox.Show(result);

110,534

社区成员

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

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

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