How do I format so the output contains '{'

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

This is mainly a string formatting question. I wasn't able to search on '{' or '@' so I am turning to this group for what should be a very simple question

Primarily for regular expressions I want to format a string that has '{' characters in the output. Something like

string.Format(@"(?<char>[A-Za-z0-9])(\k<char>){{0},}", count - 1)

To find repeating characters. The problem is that the above does not work. I don't know how to escape the '{' character? As is the above throws an exception

Any ideas

Kevin
 
Use two of them {{

Kevin Burton said:
This is mainly a string formatting question. I wasn't able to search on
'{' or '@' so I am turning to this group for what should be a very simple
question.
Primarily for regular expressions I want to format a string that has '{'
characters in the output. Something like:
string.Format(@"(?<char>[A-Za-z0-9])(\k<char>){{0},}", count - 1);

To find repeating characters. The problem is that the above does not work.
I don't know how to escape the '{' character? As is the above throws an
exception.
 
Kevin said:
This is mainly a string formatting question. I wasn't able to search on '{' or '@' so I am turning to this group for what should be a very simple question.

Primarily for regular expressions I want to format a string that has '{' characters in the output. Something like:

string.Format(@"(?<char>[A-Za-z0-9])(\k<char>){{0},}", count - 1);

To find repeating characters. The problem is that the above does not work. I don't know how to escape the '{' character? As is the above throws an exception.

Any ideas?

Simply double up the braces to get them formatted as literals.

Your format string should look like:

@"(?<char>[A-Za-z0-9])(\k<char>){{{0},}}"

There are times when this doesn't work, however. For details, see:

http://blogs.msdn.com/brada/archive/2004/01/14/58851.aspx
 
Back
Top