1

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
  • regex html strikes again Commented Feb 26, 2009 at 12:41

4 Answers 4

4

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;
Sign up to request clarification or add additional context in comments.

Comments

1

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

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

2 Comments

i do that but nothing returend
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.
1

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.