积极答复者
请教匹配语句的写法和引用匹配集值的方法

问题
-
string code = "fuid=19064515" class="sl">萧大</a><br />
已住1小时47分</div>"
这个字符串带了换行回车,我想匹配 19064515 萧大 1小时47分
Regex regex = new Regex("fuid=(?<uid>\\d+)\" class=\"sl\">(?<name>.{1,20})</a><br />\n\r .{0,25}已住(?<time>.{1,100})</div>");
MatchCollection matchs = regex.Matches(code);
每回获取的值都是空,这个匹配语句应该怎么写?
还有,除了用下面这个方法引用匹配集的值
还有别的方法吗?
foreach (Match m in matchs)
{
string liveuid=m.Groups["uid"].Value;
if (liveuid == tempuid)
{
time = m.Groups["time"].Value;
}
}- 已移动 Sheng Jiang 蒋晟Moderator 2009年5月13日 18:17 .Net基础类库问题 ([Loc]From:Visual C#)
答案
全部回复
-
修改下面的代码实现你的需求,读一下Regular Expression的文章:
http://www.codeproject.com/KB/dotnet/regextutorial.aspx
http://www.codeproject.com/KB/dotnet/expresso.aspx
private void button1_Click(object sender, EventArgs e)
{
string code = "fuid=19064515\" class=\"sl\">萧大</a><br />已住1小时47分</div>";
Regex rex = new Regex(@"fuid=\d{8}\""\s*class=\""sl\""\>萧大");
MatchCollection matchs = rex.Matches(code);
MessageBox.Show(matchs.Count.ToString());
}
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.