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 8796523
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:38:36+00:00 2026-06-13T23:38:36+00:00

Given the following scenario, I am wondering if a better solution could be written

  • 0

Given the following scenario, I am wondering if a better solution could be written with Regular Expressions for which I am not very familiar with yet. I am seeing holes in my basic c# string manipulation even though it somewhat works. Your thoughts and ideas are most appreciated.

Thanks much,

Craig

Given the string “story” below, write a script to do the following:

  1. Variable text is enclosed by { }.
  2. If the variable text is blank, remove any other text enclosed in [ ].
  3. Text to be removed can be nested deep with [ ].

Format:

    XYZ Company [- Phone: [({404}) ]{321-4321} [Ext: {6789}]]

Examples:

  1. All variable text filled in.

    XYZ Company - Phone: (404) 321-4321 Ext: 6789
    
  2. No Extension entered, remove “Ext:”.

    XYZ Company - Phone: (404) 321-4321
    
  3. No Extension and no area code entered, remove “Ext:” and “( ) “.

    XYZ Company - Phone: 321-4321
    
  4. No extension, no phone number, and no area code, remove “Ext:” and “( ) ” and “- Phone: “.

    XYZ Company
    

Here is my solution with plain string manipulation.

private string StoryManipulation(string theStory)
    {
        // Loop through story while there are still curly brackets
        while (theStory.IndexOf("{") > 0)
        {
            // Extract the first curly text area
            string lcCurlyText = StringUtils.ExtractString(theStory, "{", "}");                

            // Look for surrounding brackets and blank all text between
            if (String.IsNullOrWhiteSpace(lcCurlyText))
            {
                for (int lnCounter = theStory.IndexOf("{"); lnCounter >= 0; lnCounter--)
                {
                    if (theStory.Substring(lnCounter - 1, 1) == "[")
                    {
                        string lcSquareText = StringUtils.ExtractString(theStory.Substring(lnCounter - 1), "[", "]");
                        theStory = StringUtils.ReplaceString(theStory, ("[" + lcSquareText + "]"), "", false);
                        break;
                    }
                }
            }
            else
            {
                // Replace current curly brackets surrounding the text
                theStory = StringUtils.ReplaceString(theStory, ("{" + lcCurlyText + "}"), lcCurlyText, false);
            }
        }
        // Replace all brackets with blank (-1 all instances)
        theStory = StringUtils.ReplaceStringInstance(theStory, "[", "", -1, false);
        theStory = StringUtils.ReplaceStringInstance(theStory, "]", "", -1, false);
        return theStory.Trim();
    }
  • 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-13T23:38:38+00:00Added an answer on June 13, 2026 at 11:38 pm

    Dealing with nested structures is generally beyond the scope of regular expressions. But I think there is a solution, if you run the regex replacement in a loop, starting from the inside out. You will need a callback-function though (a MatchEvaluator):

    string ReplaceCallback(Match match)
    {
        if(String.IsNullOrWhiteSpace(match.Groups[2])
            return "";
        else
            return match.Groups[1]+match.Groups[2]+match.Groups[3];
    }
    

    Then you can create the evaluator:

    MatchEvaluator evaluator = new MatchEvaluator(ReplaceCallback);
    

    And then you can call this in a loop until the replacement does not change anything any more:

    newString = Regex.Replace(
        oldString,
        @"
        \[    # a literal [
        (     # start a capturing group. this is what we access with "match.Groups[1]"
            [^{}[\]]
              # a negated character class, that matches anything except {, }, [ and ]
            * # arbitrarily many of those
        )     # end of the capturing group
        \{    # a literal {
        ([^{}[\]]*)
              # the same thing as before, we will access this with "match.Groups[2]"
        }     # a literal }
        ([^{}[\]]*)
              # "match.Groups[3]"
        ]     # a literal ]
        ",
        evaluator,
        RegexOptions.IgnorePatternWhitespace
    );
    

    Here is the whitespace-free version of the regex:

    \[([^{}[\]]*)\{([^{}[\]]*)}([^{}[\]]*)]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was wondering what an efficient algorithm would be in the following scenario: Given
Given the following scenario, which one of the following is preferred. m_state is a
The following scenario is given: /Collection1 (a number of users are subscribed to this
I am running the following feature: Scenario: viewing existing images Given I am on
I have the following scenarios: Scenario: Create a game with valid information Given I
I'm wondering if anyone knows how exactly Gmail, Hotmail, Facebook etc handles following scenario.
Given the following scenario, I would like to know if functionOneLock releases itself before
I have the following scenario: Editor Role should not be allowed to delete nodes.
I'd like to know what happens given the following scenario using the .net client.
The following scenario pretty much sums up my problem: Scenario: problems with subprocesses Given

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.