Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8622799
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T07:08:22+00:00 2026-06-12T07:08:22+00:00

I have strings that I read from the database, these strings are fed into

  • 0

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.

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.

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 ?

A string example is the following:

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

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T07:08:24+00:00Added an answer on June 12, 2026 at 7:08 am

    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+}.

    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 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}";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string I'm pulling from a Filemaker database that's supposed to read
I want to read UTF-8 strings from a server that I have control of,
I have the problem that I must read Integers from a database. The varibles
I have a string that I want to have PHP read as a piece
I have read that to match a word inside of a string using Regular
I have read in a Java book that says: Because a String is immutable,
say i have strings that are sent back and forth from the client, and
We have a requirement from customer that if someone gets access to the database,
I have to read image binary from database and save this image binary as
I have a large theoretical string (104 characters long) database generation program that returns

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.