0

我正在使用 webrequest 从页面下载源,然后我需要使用 Regex 来获取字符串并将其存储在字符串中:

U_nQgAjU_tdUnfcA7lT5opoTLyLdslWDTpiNzcdkLoHlobS_HbujMw..

还需要:

bpvsid=nvnN2JFJqJc.&dcz=1

两者都出:

<td style="cursor:pointer;" class="" onclick="NewWindow('U_nQgAjU_tdUnfcA7lT5opoTLyLdslWDTpiNzcdkLoHlobS_HbujMw..', 'bpvsid=nvnN2JFJqJc.&dcz=1', 'bpvstage_edit', '1200', '800')" onmouseout="HideHover();"><img src="gfx/info.gif" alt="" tipwidth="450" ajaxtip="openajax.php?target=modules/bpv/bpvstage_hover_info.php&rid=&oid=&bpvsid=&bpvname=" /></td>

它不断给我错误,比如不够)?

提前致谢。

当前的代码,可能在各个方面都是错误的。对此真的很陌生:

 Regex rx = new Regex("(?<=class=\"\" onclick=\"NewWindow(').*(?=')");
            longId = (rx.Match(textBox2.Text).Value);
            textBox1.Text = longId;
4

4 回答 4

0

正则表达式NewWindow\('([^']*)', '([^']*)将符合您的要求。所需的两个字符串将位于 Groups[1] 和 Groups[2] 中。

var match = Regex.Match(textBox2.Text, "NewWindow\('([^']*)', '([^']*)");
var id1 = match.Groups[1].Value;
var id2 = match.Groups[2].Value;
于 2013-11-05T10:56:06.117 回答
0
var match = Regex.Match(s, @"onclick=""NewWindow\('([^']*)',\s*'([^']*)',.*");
if (match.Success)
{
    string longId = match.Groups[1].Value;
    string other = match.Groups[2].Value;
}

这将为您提供两个具有值的组:

U_nQgAjU_tdUnfcA7lT5opoTLyLdslWDTpiNzcdkLoHlobS_HbujMw..
bpvsid=nvnN2JFJqJc.&dcz=1
于 2013-11-05T10:57:56.407 回答
0

请注意,您也可以使用简单的字符串函数而不是正则表达式:

var s = "<td style=\"cursor:pointer;\" class=\"\" onclick=\"NewWindow('U_nQgAjU_tdUnfcA7lT5opoTLyLdslWDTpiNzcdkLoHlobS_HbujMw..', 'bpvsid=nvnN2JFJqJc.&dcz=1', 'bpvstage_edit', '1200', '800')\" onmouseout=\"HideHover();\"><img src=\"gfx/info.gif\" alt=\"\" tipwidth=\"450\" ajaxtip=\"openajax.php?target=modules/bpv/bpvstage_hover_info.php&rid=&oid=&bpvsid=&bpvname=\" /></td>";
var tmp = s.Substring(s.IndexOf("NewWindow('")).Split('\'');
var value1 = tmp[1]; // U_nQgAjU_tdUnfcA7lT5opoTLyLdslWDTpiNzcdkLoHlobS_HbujMw..
var value2 = tmp[3]; // bpvsid=nvnN2JFJqJc.&dcz=1
于 2013-11-05T11:00:11.757 回答
0

我会HtmlAgilityPack用来解析 HTML,然后这种非正则表达式方法有效:

string html = // get your html ...
var doc = new HtmlAgilityPack.HtmlDocument();  
doc.LoadHtml(html);  // doc.Load can also consume a response-stream directly
var result = Enumerable.Empty<string>();
var firstTD = doc.DocumentNode.SelectNodes("//td").FirstOrDefault();
if (firstTD != null)
{
    if (firstTD.Attributes.Contains("onclick"))
    {
        string onclick = firstTD.Attributes["onclick"].Value;
        int newWindowIndex = onclick.IndexOf("newWindow(", StringComparison.OrdinalIgnoreCase);
        if (newWindowIndex >= 0)
        {
            string functionBody = onclick.Substring(newWindowIndex + "newWindow(".Length);
            string[] tokens = functionBody.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            result = tokens.Take(2).Select(s => s.Trim(' ', '\''));
        }
    }
}
于 2013-11-05T11:02:54.343 回答