在字符串中转义大括号{以支持string . format

时间:2020-12-16 00:08:28

I have strings that I read from the database, these strings are fed into String.Format method, if a string has '{' '}' braces but these braces are not escaped correctly for String.Format (i.e add another '{' to escape them) the String.Format will throw an exception.

我有从数据库中读取的字符串,这些字符串被输入到字符串中。格式方法,如果一个字符串有'{' '}'括号,但是这些括号对于字符串没有正确转义。格式(我。e添加另一个“{”以转义它们)字符串。格式将抛出异常。

The string have any combination of these braces, so in essence the method needs to go through the string and figure out if '{' have a closing one and if together they form a valid place holder for String.Format (i.e. {5}), the ones that don't need to be escaped correctly.

字符串有这些大括号的任何组合,所以本质上,方法需要遍历字符串,并确定'{'是否有一个闭括号,如果它们一起构成了字符串的有效位置保持符。格式(即{5}),不需要正确转义的格式。

I can write a method to do that, but was wondering if there is anything built into .NET, or anything out there that already does this ?

我可以编写一个方法来实现这一点,但是我想知道。net中是否有什么内置的东西,或者有什么已经实现的东西?

A string example is the following:

一个字符串示例如下:

Hello {0}, please refer to our user manual for more information {or contact us at: XXXX}"

你好{0},更多信息请参考我们的用户手册{或联系我们:XXXX}

As you can tell feeding this into String.Format will throw an exception on the {or contact us at: XXXX}

就像你看到的那样,把这个输入到字符串中。格式将在{或在:XXXX}联系我们时抛出异常

3 个解决方案

#1


2  

How about this:

这个怎么样:

string input = "Hello {0}, please refer for more information {or contact us at: XXXX}";
   Regex rgx = new Regex("(\\{(?!\\d+})[^}]+})");
string replacement = "{$1}";
string result = rgx.Replace(input, replacement);

Console.WriteLine("String {0}", result);

// Hello {0}, please refer for more information {{or contact us at: XXXX}}

... assuming that the only strings that should not be escaped are of format {\d+}.

…假设唯一不能转义的字符串是格式{\d+}。

There are two caveats here. First, we may encounter already-escaped curvy braces - {{. Fix for it is more-o-less easy: add more lookarounds...

这里有两个注意事项。首先,我们可能会遇到已经转义的弯曲大括号{。解决这个问题更不容易:增加更多的查找……

Regex rgx = new Regex("(\\{(?!\\d+})(?!\\{)(?<!\\{\\{)[^}]+})");

... in other words, when trying to replace a bracket, make sure it's a lonely one. )

…换句话说,当试图替换括号时,要确保它是一个孤独的括号。

Second, formats themselves may not be so simple - and as the complexity of those that actually may be present in your strings grows, so does the regex's one. For example, this little beasty won't attempt to fix format string that start with numbers, then go all the way up to } symbol without any space in between:

其次,格式本身可能不那么简单——随着字符串中实际存在的格式的复杂性增加,regex的格式也会随之增加。例如,这个小的beasty不会试图修复以数字开头的格式字符串,然后一直到}符号,中间没有任何空格:

Regex rgx = new Regex("(\\{(?!\\d\\S*})(?!\\{)(?<!\\{\\{)[^}]+})");

As a sidenote, even though I'm the one of those people that actually like to have two problems (well, sort of), I do shudder when I look at this.

作为一个旁注,尽管我是那种喜欢有两个问题的人(嗯,有点),但当我看到这个时,我确实会感到不寒而栗。

UPDATE: If, as you said, you need to escape each single occurrence of { and }, your task is actually a bit easier - but you will need two passes here, I suppose:

更新:如果,如您所说,您需要摆脱{和}的每一个事件,您的任务实际上是比较容易的—但是您需要两个通过,我想:

Regex firstPass = new Regex("(\\{(?!\\d+[^} ]*}))");
string firstPassEscape = "{$1";
...
Regex secondPass        = new Regex("((?<!\\{\\d+[^} ]*)})");
string secondPassEscape = "$1}";

#2


0  

Is it possible to use a different escape character for your custom processing tool? Because you you chose one that is already taken!.

是否可以为自定义处理工具使用不同的转义字符?因为你选择了一个已经被拿走的!

If it is not possible, then other option is to escape it when storing it. It will be un-escaped automatically when the string.Format ends.

如果不可能,那么另一种选择是在存储时转义它。当字符串时,它将被自动转义。格式的目的。

var s = string.Format("Hello {0} please ... {{or contact}}", customer.Name );
// Now use s as needed 
// now is: "Hello Joe please ... {or contact}"

#3


0  

Try:

试一试:

myString = Regex.Replace(myString, @"\{(\D+)\}", "{{$1}}");

#1


2  

How about this:

这个怎么样:

string input = "Hello {0}, please refer for more information {or contact us at: XXXX}";
   Regex rgx = new Regex("(\\{(?!\\d+})[^}]+})");
string replacement = "{$1}";
string result = rgx.Replace(input, replacement);

Console.WriteLine("String {0}", result);

// Hello {0}, please refer for more information {{or contact us at: XXXX}}

... assuming that the only strings that should not be escaped are of format {\d+}.

…假设唯一不能转义的字符串是格式{\d+}。

There are two caveats here. First, we may encounter already-escaped curvy braces - {{. Fix for it is more-o-less easy: add more lookarounds...

这里有两个注意事项。首先,我们可能会遇到已经转义的弯曲大括号{。解决这个问题更不容易:增加更多的查找……

Regex rgx = new Regex("(\\{(?!\\d+})(?!\\{)(?<!\\{\\{)[^}]+})");

... in other words, when trying to replace a bracket, make sure it's a lonely one. )

…换句话说,当试图替换括号时,要确保它是一个孤独的括号。

Second, formats themselves may not be so simple - and as the complexity of those that actually may be present in your strings grows, so does the regex's one. For example, this little beasty won't attempt to fix format string that start with numbers, then go all the way up to } symbol without any space in between:

其次,格式本身可能不那么简单——随着字符串中实际存在的格式的复杂性增加,regex的格式也会随之增加。例如,这个小的beasty不会试图修复以数字开头的格式字符串,然后一直到}符号,中间没有任何空格:

Regex rgx = new Regex("(\\{(?!\\d\\S*})(?!\\{)(?<!\\{\\{)[^}]+})");

As a sidenote, even though I'm the one of those people that actually like to have two problems (well, sort of), I do shudder when I look at this.

作为一个旁注,尽管我是那种喜欢有两个问题的人(嗯,有点),但当我看到这个时,我确实会感到不寒而栗。

UPDATE: If, as you said, you need to escape each single occurrence of { and }, your task is actually a bit easier - but you will need two passes here, I suppose:

更新:如果,如您所说,您需要摆脱{和}的每一个事件,您的任务实际上是比较容易的—但是您需要两个通过,我想:

Regex firstPass = new Regex("(\\{(?!\\d+[^} ]*}))");
string firstPassEscape = "{$1";
...
Regex secondPass        = new Regex("((?<!\\{\\d+[^} ]*)})");
string secondPassEscape = "$1}";

#2


0  

Is it possible to use a different escape character for your custom processing tool? Because you you chose one that is already taken!.

是否可以为自定义处理工具使用不同的转义字符?因为你选择了一个已经被拿走的!

If it is not possible, then other option is to escape it when storing it. It will be un-escaped automatically when the string.Format ends.

如果不可能,那么另一种选择是在存储时转义它。当字符串时,它将被自动转义。格式的目的。

var s = string.Format("Hello {0} please ... {{or contact}}", customer.Name );
// Now use s as needed 
// now is: "Hello Joe please ... {or contact}"

#3


0  

Try:

试一试:

myString = Regex.Replace(myString, @"\{(\D+)\}", "{{$1}}");