[英]Regex help need
I am trying to replace model => model.Password
substring within @Html.DisplayNameFor(model => model.Password)
. 我试图在
@Html.DisplayNameFor(model => model.Password)
替换model => model.Password
子字符串。
Any clue which regex template I have use to? 我使用的正则表达式模板的任何线索?
I have tried \\\\(.+?\\\\)
Is this correct? 我试过
\\\\(.+?\\\\)
这是对的吗?
Thank you! 谢谢!
var match = Regex.Match(view, @"@Html.DisplayNameFor(\\(.+?\\))", RegexOptions.IgnoreCase);
if (match.Success)
{
}
The pattern should be more like: 模式应该更像:
@"@Html\.DisplayNameFor\((.+?)\)"
Since you were already using a verbatim string ( @""
), you shouldn't have included the double backslashes. 由于您已经使用了逐字符串(
@""
),因此不应包含双反斜杠。 Also, the .
而且,
.
should have been escaped, and I adjusted the grouping parens to match model => model.Password
instead of (model => model.Password)
. 应该已经转义,我调整了分组的parens以匹配
model => model.Password
而不是(model => model.Password)
。
And if you're trying to replace, you may want to just use Regex.Replace
directly instead of first Match
ing it. 如果您正在尝试替换,您可能只想直接使用
Regex.Replace
而不是首先Match
它。
The problem is in this statement: 问题出在这个声明中:
var match = Regex.Match(view, @"@Html.DisplayNameFor(\\(.+?\\))",
RegexOptions.IgnoreCase);
You used @
before the regex pattern to escape all characters. 您在正则表达式模式之前使用
@
来转义所有字符。 In this case, it's not necessary escape your pattern again using one more "\\"
. 在这种情况下,没有必要再使用一个
"\\"
来逃避您的模式。 So, this code should be more like: 所以,这段代码应该更像:
var match = Regex.Match(view, @"@Html.DisplayNameFor(\(.+?\))",
RegexOptions.IgnoreCase);
Or you can remove the @
to continue using double slashes: 或者您可以删除
@
以继续使用双斜杠:
var match = Regex.Match(view, "@Html.DisplayNameFor(\\(.+?\\))",
RegexOptions.IgnoreCase);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.