简体   繁体   中英

Regex help need

I am trying to replace model => model.Password substring within @Html.DisplayNameFor(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) .

And if you're trying to replace, you may want to just use Regex.Replace directly instead of first Match ing it.

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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