我如何使用Regex从中获取某个字符串?

时间:2013-11-05 10:46:12

标签: c# regex split

我正在使用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 个答案:

答案 0 :(得分: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;

答案 1 :(得分: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

答案 2 :(得分: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

答案 3 :(得分: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(' ', '\''));
        }
    }
}