I'm trying to replace a string,enclosed in curly brackets.
If I use the Replace method provided by the Regex class and I don't specify the curly brackets, the string is found and replaced correctly, but if I do specify the curly brackets like this: {{FullName}}
, the text is left untouched.
var pattern = "{{" + keyValue.Key + "}}";
docText = new Regex(pattern, RegexOptions.IgnoreCase).Replace(docText, keyValue.Value);
Take this string as a example
Dear {{FullName}}
I want to replace it with John
, so that the text ends up like this:
Dear John
.
How can I express the regex, so that the string is found and replace correctly?
You don't need a regular expression if the key is just a string. Just replace "{{FullName}}" with "John". example:
string template = "Dear {{FullName}}";
string result = template.Replace("{{" + keyValue.Key + "}}", keyValue.Value);
Edit: addressing concerns that this doesn't work...
The following is a complete example. You can run it at https://dotnetfiddle.net/wnIkvf
using System;
using System.Text.RegularExpressions;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
var keyValue = new KeyValuePair<string,string>("FullName", "John");
string docText = "Dear {{FullName}}";
string result = docText.Replace("{{" + keyValue.Key + "}}", keyValue.Value);
Console.WriteLine(result);
}
}
Looking for "Dear {{FullName}}"
to "Dear John"
?
not a regex solution... but this is how I prefer to do it sometimes.
string s = "Dear {{FullName}}";
// use regex to replace FullName like you mentioned before, then...
s.Replace("{",string.empty);
s.Replace("}",string.empty);
var keyValue = new KeyValuePair<string,string>("FullName", "John");
var pattern = "{{" + keyValue.Key + "}}";
Console.WriteLine(new Regex(Regex.Escape(pattern), RegexOptions.IgnoreCase).Replace("Dear {{FullName}}", keyValue.Value));
Output:
Dear John
If you actually want to use a regular expression, then escape your literal text to turn it into a regular expression pattern using Regex.Escape .
var keyValue = new KeyValuePair<string,string>("FullName", "John");
string docText = "Dear {{FullName}}";
var pattern = "{{" + keyValue.Key + "}}";
docText = new Regex(Regex.Escape(pattern), RegexOptions.IgnoreCase).Replace(docText, keyValue.Value);
docText will be Dear John
What I believe is that you really want to do is replace multiple things in a document.
To do so use the regex pattern I provide, but also use the regex replace match evaluator delegate. What that does, is that every match can be actively evaluated for each item and a proper item will be replaced as per C# logic.
Here is an example with two possible keywords setup.
string text = "Dear {{FullName}}, I {{UserName}} am writing to say what a great answer!";
string pattern = @"\{\{(?<Keyword>[^}]+)\}\}";
var replacements
= new Dictionary<string, string>() { { "FullName", "OmegaMan" }, { "UserName", "eddy" } };
Regex.Replace(text, pattern, mt =>
{
return replacements.ContainsKey(mt.Groups["Keyword"].Value)
? replacements[mt.Groups["Keyword"].Value]
: "???";
}
);
Result
Dear OmegaMan, I eddy am writing to say what a great answer!
The preceding example uses
(?<{Name here}> …)
[^ ]
which says match until the negated item is found, in this case a closing curly }
.
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.