简体   繁体   English

在带有命名组的正则表达式中不会发生预期结果

[英]Expected result not happens in this regular expression with named groups

I have this template: 我有这个模板:

Dear @Model["ClientName"] <br><br>

           Thank you for applying to open an account. Your request for a (@Model["AccountType"]/@Model["Platform"]/@Model["Currency"]) has been sent to our Client team. They are currently reviewing your request.<br><br>

Kind regards,<br>
<a href="mailto:test@example.com">test@example.com</a><br />
+55 00 0000 0000 <br>08:00-18:00 Mon-Fri

And would like to replace all @Model["key"] with a string value. 并希望将所有@Model["key"]替换为字符串值。

I have this regex: 我有这个正则表达式:

const string pattern = @".*(?<model>@Model\[\u0022(?<key>\w+)\u0022\]).*";
var matches = Regex.Matches(input, pattern, RegexOptions.IgnoreCase);

When I check the matches collection I find only 2, ClientName and Currency. 当我检查比赛集合时,我发现只有2个,ClientName和Currency。

Why is that? 这是为什么? I was expecting to find AccountType and Platform too. 我也期望找到AccountType和Platform。

Can you help me to identify what is wrong? 您能帮我找出问题所在吗?

Try this match pattern (without the .* before and after): 尝试以下匹配模式(前后不带.* ):

const string pattern = @"(?<model>@Model\[\u0022(?<key>\w+)\u0022\])";

This will stop the .* from greedily swallowing the whole line, and considering it to be a single match. 这将阻止.*贪婪地吞下整行,并将其视为单个匹配项。

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

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