如何用正则表达替换? 两个问题我获取一个页面1,我想得到从<--title-->,截至到<--/title/-->之间得内容2,我想把<dd>.........</dd>之间得值获取出来,并且这样的有多个 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 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(); } 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(); } 1,我想得到从<--title-->,截至到<--/title/-->之间得内容如果包含有引号呢?比如<--"title"--> 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参数有时候加了反而会适得其反 C#读写excel文件的问题 ListBox中怎么改变某一行的背景色 winform 怎么在日历上显示对应内容 一个简单的问题,大家进来看看啊 asp.net+c#求助 C# 把十六进制转成字符串 关于WIN控件拖动大小的问题! 如何让dll成为公用的dll? 对象序列化到byte数组 有趣的其他进制问题 没有堆栈如何"全部展开"
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();
}
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();
}
比如<--"title"-->
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参数有时候加了反而会适得其反