如何用正则表达替换?

panzi667 2007-08-06 01:13:01
两个问题
我获取一个页面
1,我想得到从<--title-->,截至到<--/title/-->之间得内容
2,我想把<dd>.........</dd>之间得值获取出来,并且这样的有多个
...全文
170 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
-过客- 2007-08-06
  • 打赏
  • 举报
回复
1、
string yourStr = ..............;
Match m = Regex.Match(yourStr, @"<--title-->([\s\S]*?)<--/title/-->", RegexOptions.IgnoreCase);
if (m.Success)
{
MessageBox.Show(m.Groups[1].Value);
}

2、
string yourStr = ..............;
MatchCollection mc = Regex.Matches(yourStr, @"<dd>([\s\S]*?)</dd>", RegexOptions.IgnoreCase);
foreach (Match m in mc)
{
richTextBox2.Text += m.Groups[1].Value + "\n";
}



包含引号那就加上引号就行了
string yourStr = ..............;
Match m = Regex.Match(yourStr, @"<--""title""-->([\s\S]*?)<--/title/-->", RegexOptions.IgnoreCase);
if (m.Success)
{
MessageBox.Show(m.Groups[1].Value);
}


To:bdbox
你写的也可以,只是这里Multiline参数没有意义,可以不加,有些参数不需要加的时候最好不要加,比如Compiled参数有时候加了反而会适得其反
panzi667 2007-08-06
  • 打赏
  • 举报
回复
UP
panzi667 2007-08-06
  • 打赏
  • 举报
回复
1,我想得到从<--title-->,截至到<--/title/-->之间得内容

如果包含有引号呢?
比如<--"title"-->
bdbox@qq.com 2007-08-06
  • 打赏
  • 举报
回复
2、

string sourceString = @"<dd>w你好啊</dd><dd>asdfasdfs</dd>";
string pattern = @"(?<=<dd>)([\s\S]+?)(?=</dd>)";
System.Text.RegularExpressions.Match result = Regex.Match(sourceString,pattern,System.Text.RegularExpressions.RegexOptions.IgnoreCase|System.Text.RegularExpressions.RegexOptions.Multiline);

while(result.Success)
{
WL("正确:" + result.Value);//输出
result = result.NextMatch();
}
bdbox@qq.com 2007-08-06
  • 打赏
  • 举报
回复
1\ string sourceString = @"<--title-->,截至到<--/title/-->";
string pattern = @"(?<=<--title-->)([\s\S]+?)(?=<--/title/-->)";
System.Text.RegularExpressions.Match result = Regex.Match(sourceString,pattern,System.Text.RegularExpressions.RegexOptions.IgnoreCase|System.Text.RegularExpressions.RegexOptions.Multiline);

while(result.Success)
{
WL("正确:" + result.Value);//输出
result = result.NextMatch();
}

110,545

社区成员

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

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

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