简体   繁体   English

C#正则表达式问题

[英]c# Regex problem

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 我需要name =“” value和value =“” value:P创建此代码,但此代码有效

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 Form1.cs(71,69):错误CS1009:无法识别的转义序列

at this line 在这条线

Regex rloginRnd = new Regex(@"name="+ loginrand+"***value=\\w****"); 正则表达式rloginRnd =新正则表达式(@“ name =” + loginrand +“ *** value = \\ w ****”);

Regex is not always the best tool for HTML; 正则表达式并不总是HTML的最佳工具。 I'd use the HTML Agility Pack (since it isn't xhtml), and xpath - it should be pretty trivial then: 我将使用HTML Agility Pack (因为它不是xhtml)和xpath-那么它应该是很简单的:

    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;

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 注意:w前加双\\

您需要在字符串的每个部分之前添加一个@

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

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 或使用string.format

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM