tags:

views:

222

answers:

4

hi ,i want search a value in on row like this

<p align="center"><input type="hidden" name="e79e7ec" value="15302f565b">

i need name="" value and value="" value :P create this code , but this code dosent work

Regex rloginRand = new Regex(@"<p align=center><input type=hidden name=\w*");
            Match mloginRand = rloginRand.Match(source);
            string loginrand = "";
            if (mloginRand.Success)
            {
                 loginrand = mloginRand.ToString().Replace("<p align=center><input type=hidden name=", "");
            }
            string loginrnd = "";
            Regex rloginRnd = new Regex(@"name="+ loginrand+"value=\w*");
            Match mloginRnd = rloginRnd.Match(source);

            if (mloginRand.Success)
            {
                loginrnd = mloginRnd.ToString().Replace("name="+loginrand+" value=", "");
            }

error is

Form1.cs(71,69): error CS1009: Unrecognized escape sequence

at this line

Regex rloginRnd = new Regex(@"name="+ loginrand+"*value=\w**");

+1  A: 

use
Regex rloginRnd = new Regex(@"name="+ loginrand+@"*value=\w**");
Note: Second @ sign

or
Regex rloginRnd = new Regex(@"name="+ loginrand+"*value=\\w**");
Note: Double \ before w

Robert MacLean
+1  A: 

You need to put an @ before each part of the string:

Regex rloginRnd = new Regex(@"name="+ loginrand+@"value=\w*");
Andy
i do that but nothing returend
madman
I'm not positive, but I think the problem is that you don't need to search for "name=" in the second Regex, since that has already been taken care of in the first one.
Andy
A: 

I don't think the @ covers the entire string concatination, only the bit that it's on.

Try.

Regex rloginRnd = new Regex(@"name="+ loginrand+ @"*value=\w**");

or

Regex rloginRnd = new Regex(@"name="+ loginrand+ "*value=\\w**");

or use string.format

DeletedAccount
+4  A: 

Regex is not always the best tool for HTML; I'd use the HTML Agility Pack (since it isn't xhtml), and xpath - it should be pretty trivial then:

    HtmlDocument doc = new HtmlDocument();
    doc.LoadHtml(html);
    HtmlNode node = doc.DocumentNode.SelectSingleNode("input");
    // read (if you want)
    string name = node.GetAttributeValue("name", ""),
        value = node.GetAttributeValue("value", "");
    // wipe
    node.SetAttributeValue("name", loginrand);
    node.SetAttributeValue("value", "");
    // get html
    html = doc.DocumentNode.OuterHtml;
Marc Gravell