String.Format with curly braces

Our low-level logging library should handle all kinds of log messages sent to it.

Some of these messages include braces (as part of the text), and some contain parameters that must be formatted as part of the string using String.Format

For example, this line may be an entry in the Logger class:

"Parameter: {Hostname} Value: {0}" Using the correct variable sent for use for formatting.

To do this correctly, I must avoid curly braces that are not part of the formatting (by doubling them).

I was thinking of doing this with Regex, but it's not as simple as it might seem, since I have no idea how to match these strings inside curly braces (those that are NOT used by String.Format for formatting).

Another problem is that the Logger class should be as efficient as possible, starting with regular expressions, as part of its work may interfere with the work.

Is there any good and known best practice for this?

+6
source share
4 answers

Doing this in only one regular expression:

string input = "Parameter: {Hostname} Value: {0}"; input = Regex.Replace(input, @"{([^[0-9]+)}", @"{{$1}}"); Console.WriteLine(input); 

Outputs:

Parameter: {{Hostname}} Value: {0}

This, of course, only works until any parameters containing numbers are found, but should still be escaped using {{ }}

+3
source

I would double all the curly braces, and then I would look for those that need to be replaced with a regular expression, for example {{\d+}} , so that they return to the original format - {{0}} => {0} - in your line.
So for each line, I would do it like this:

 string s = input.Replace("{", "{{").Replace("}", "}}"); return Regex.Replace(s, @"{{(?<val>\d+)}}", m => { return "{" + m.Groups["val"] + "}"; })); 

So, a technical answer to the original question, but @Anders Abel is absolutely right. It would be wise to review the project again ...

+2
source

I think you should look into your logs interface. Compare with how Console.WriteLine works:

  • Console.WriteLine(String) outputs the exact specified string, without formatting, nothing special with {and}.
  • Console.WriteLine(String, Object[]) exits using formatting. {and} are special characters that the caller must escape to {{and}}

I think this is the wrong design, which must distinguish between different curly braces in the code to find out what that means. Put the burden of escape {this should happen in the output in {{.

+2
source

To allow the caller to have formatted strings and handle formatting specifications, e.g.

Logger.Log ("{0: dd / mm / yyy} {0: hh: mm: ss} {hostname} Some error {1: x4} occurred on {123Component}!", DateTime.UtcNow, 257)

You will need a regular expression:

 string input = "{0:dd/mm/yyy} {0:hh:mm:ss} {hostname} Some error {1:x4} happened on {123Component}!"; Regex reg = new Regex(@"(\{[^[0-9}]+?[^}]*\}|\{(?![0-9]+:)[^}]+?\})"); string output = reg.Replace(input, "{$1}"); Console.WriteLine(output); 

It is output:

 "{0:dd/mm/yyy} {0:hh:mm:ss} {{hostname}} Some error {1:x4} happened on {{123Component}}!" 

But again, I agree with Anders Abel that you should redesign to avoid the need for a library of magazines.

+2
source

Source: https://habr.com/ru/post/901490/


All Articles