简体   繁体   中英

How to use regex to replace exact phrases with a string

i have the following string.

*keyName:branding-SLES Relocations:(not relocatable)*keyOpenEnd

I want to repalce the *key with {" and the *keyOpenEnd with ": so i have a result string of

{"Name:branding-SLES Relocations:(not relocatable)":

So i have

output = Regex.Replace(output, @"\b"+"key"+@"\b", "{\"");
output = Regex.Replace(output, @"\b\*keyOpenEnd\b", "\":");

I have tried various diff combinations but nothing works.

Update: Seems to be a bit of confusion on question. To clarify; I need to replace exact phrases else it will replace 'Key' in KeyOpenEnd aswell as Key which is not good. I need exact phrase replacement.

Why not just use string.Replace and make sure you replace the more specific value first

output = output.Replace("*keyOpenEnd", "\":").Replace("*key", "{\"");

EDIT

Here's test code to compare regular expression time versus string.Replace and my results

string s = "*keyName:branding-SLES Relocations:(not relocatable)*keyOpenEnd";
string desired = "{\"Name:branding-SLES Relocations:(not relocatable)\":";

Stopwatch watch = new Stopwatch();

watch.Start();
for (int i = 0; i < 100000; i++)
{
    var n = Regex.Replace(s, @"\*keyOpenEnd", "\":");
    n = Regex.Replace(n, @"\*key", "{\"");
    Assert.AreEqual(desired, n);
}
watch.Stop();

Console.WriteLine("RegEx Total: {0}", watch.Elapsed);

watch.Reset();
watch.Start();
for (int i = 0; i < 100000; i++)
{
    var n = s.Replace("*keyOpenEnd", "\":").Replace("*key", "{\"");
    Assert.AreEqual(desired, n);
}
watch.Stop();

Console.WriteLine("String Replace Total: {0}", watch.Elapsed);

Results:

RegEx Total: 00:00:00.1742555
String Replace Total: 00:00:00.0385957

Additional Edit

If you use one regular expression and compile it up front for use, string.Replace is still faster

string s = "*keyName:branding-SLES Relocations:(not relocatable)*keyOpenEnd";
string desired = "{\"Name:branding-SLES Relocations:(not relocatable)\":";
Regex r = new Regex(@"\*key(.*)\*keyOpenEnd", RegexOptions.Compiled);

Stopwatch watch = new Stopwatch();

watch.Start();
for (int i = 0; i < 100000; i++)
{
    var n = r.Replace(s, "{\"$1\":");
    Assert.AreEqual(desired, n);
}
watch.Stop();

Console.WriteLine("RegEx Total: {0}", watch.Elapsed);

watch.Reset();
watch.Start();
for (int i = 0; i < 100000; i++)
{
    var n = s.Replace("*keyOpenEnd", "\":").Replace("*key", "{\"");
    Assert.AreEqual(desired, n);
}
watch.Stop();

Console.WriteLine("String Replace Total: {0}", watch.Elapsed);

Result:

RegEx Total: 00:00:00.0960996
String Replace Total: 00:00:00.0393491

Try replace ^\\*key with { and \\*keyOpenEnd$ with }

Edited, if * is a wild card then do the following

replace key(.*)keyOpenEnd with {"$1"

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