regex中的$0有问题。取代- c #

时间:2021-12-04 18:07:04
string value = "From: $0-$100";
string emailBody = "{From}";
emailBody = Regex.Replace(emailBody, "{From}", value, RegexOptions.IgnoreCase);

I want to get value of emailBody after regex.replace.

我想在regex.replace后获得邮件体的值。

Expected Output: From: $0-$100

预期输出::0 - 100美元

Actual Output: From: {From} - $100

实际输出:From: {From} - $100

Can someone please explain me what is the reason and how can I solve this? Thanks in Advance.

谁能给我解释一下原因,我该怎么解决?提前谢谢。

4 个解决方案

#1


2  

Use $$ in the replacement pattern to replace with 1 $: string value = "From: $$0-$$100".

在替换模式中使用$替换为1 $:string value = "From: $0-$ 100"。

string value = "From: $$0-$$100";
string emailBody = "{From}";
emailBody = Regex.Replace(emailBody, "{From}", value, RegexOptions.IgnoreCase);

The $0 in your pattern is replaced with the whole match value and the $1 is parsed as backreference to a non-existent group with ID=1, so, since it does not exist, the literal $1 is passed to the result (as you expect).

模式中的$0被替换为整个匹配值,$1被解析为对ID=1的不存在组的回引用,因此,由于不存在,字面$1被传递给结果(正如您所期望的)。

Note that if you have a dynamic (user-generated) replacement pattern that should be always treated as a literal, just Replace("$", "$$"):

注意,如果您有一个动态的(用户生成的)替换模式,它应该始终被视为文字,只需替换(“$”、“$”):

string value = "From: $0-$100";
string emailBody = "{From}";
emailBody = Regex.Replace(emailBody, "{From}", value.Replace("$", "$$"), RegexOptions.IgnoreCase);
Console.WriteLine(emailBody); // => From: $0-$100

See the C# online demo

参见c#在线演示

#2


0  

You need to escape curly braces.

你需要转义花括号。

emailBody = Regex.Replace(emailBody, @"\{From\}", value, RegexOptions.IgnoreCase);

#3


0  

If you just want to do a simple substitution (and you have user provided content), you can use the following form:

如果您只是想做一个简单的替换(并且您有用户提供的内容),您可以使用以下形式:

MyRegex.Replace(userContent, _ => "replacement");

Here we're passing a lambda that doesn't care about the matches and just explicitly returns the fixed replacement text for them.

在这里,我们传递一个不关心匹配的lambda,只是显式地为它们返回固定的替换文本。

#4


0  

You need to scape the $ sign, since it is used to substitute your capturing classes. I mean, $0, $1 .. $9 mean that you are replacing it by one of your captured classes. See here https://msdn.microsoft.com/en-us/library/az24scfc(v=vs.110).aspx

您需要设置$符号,因为它用于替换捕获类。我是说,0美元,1美元。$9表示您正在用一个捕获的类替换它。看到https://msdn.microsoft.com/en-us/library/az24scfc(v = vs.110). aspx

You scape them using another $

您使用另一个$设置它们

string value = "From: $$0-$$100";

EDIT:

编辑:

Concerning the "group 0" - the $0 thing - check here https://*.com/a/2248328/4911877

关于“组0”——$0的东西——在这里检查https://*.com/a/2248328/4911877

#1


2  

Use $$ in the replacement pattern to replace with 1 $: string value = "From: $$0-$$100".

在替换模式中使用$替换为1 $:string value = "From: $0-$ 100"。

string value = "From: $$0-$$100";
string emailBody = "{From}";
emailBody = Regex.Replace(emailBody, "{From}", value, RegexOptions.IgnoreCase);

The $0 in your pattern is replaced with the whole match value and the $1 is parsed as backreference to a non-existent group with ID=1, so, since it does not exist, the literal $1 is passed to the result (as you expect).

模式中的$0被替换为整个匹配值,$1被解析为对ID=1的不存在组的回引用,因此,由于不存在,字面$1被传递给结果(正如您所期望的)。

Note that if you have a dynamic (user-generated) replacement pattern that should be always treated as a literal, just Replace("$", "$$"):

注意,如果您有一个动态的(用户生成的)替换模式,它应该始终被视为文字,只需替换(“$”、“$”):

string value = "From: $0-$100";
string emailBody = "{From}";
emailBody = Regex.Replace(emailBody, "{From}", value.Replace("$", "$$"), RegexOptions.IgnoreCase);
Console.WriteLine(emailBody); // => From: $0-$100

See the C# online demo

参见c#在线演示

#2


0  

You need to escape curly braces.

你需要转义花括号。

emailBody = Regex.Replace(emailBody, @"\{From\}", value, RegexOptions.IgnoreCase);

#3


0  

If you just want to do a simple substitution (and you have user provided content), you can use the following form:

如果您只是想做一个简单的替换(并且您有用户提供的内容),您可以使用以下形式:

MyRegex.Replace(userContent, _ => "replacement");

Here we're passing a lambda that doesn't care about the matches and just explicitly returns the fixed replacement text for them.

在这里,我们传递一个不关心匹配的lambda,只是显式地为它们返回固定的替换文本。

#4


0  

You need to scape the $ sign, since it is used to substitute your capturing classes. I mean, $0, $1 .. $9 mean that you are replacing it by one of your captured classes. See here https://msdn.microsoft.com/en-us/library/az24scfc(v=vs.110).aspx

您需要设置$符号,因为它用于替换捕获类。我是说,0美元,1美元。$9表示您正在用一个捕获的类替换它。看到https://msdn.microsoft.com/en-us/library/az24scfc(v = vs.110). aspx

You scape them using another $

您使用另一个$设置它们

string value = "From: $$0-$$100";

EDIT:

编辑:

Concerning the "group 0" - the $0 thing - check here https://*.com/a/2248328/4911877

关于“组0”——$0的东西——在这里检查https://*.com/a/2248328/4911877

相关文章