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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T07:54:24+00:00 2026-06-10T07:54:24+00:00

Given text such as: This is my [position]. Here are some items: [items] [item]

  • 0

Given text such as:

This is my [position].
Here are some items:
[items]
    [item]
         Position within the item: [position]
    [/item]
[/items]

Once again, my [position].

I need to match the first and last [position], but not the [position] within [items]...[/items]. Is this doable with a regular expression? So far, all I have is:

Regex.Replace(input, @"\[position\]", "replacement value")

But that is replacing more than I want.

  • 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-10T07:54:26+00:00Added an answer on June 10, 2026 at 7:54 am

    As Wug mentioned, regular expressions aren’t great at counting. An easier option would be to just find the locations of all of the tokens you’re looking for, and then iterate over them and construct your output accordingly. Perhaps something like this:

    public string Replace(input, replacement)
    {
        // find all the tags
        var regex = new Regex("(\[(?:position|/?item)\])");
        var matches = regex.Matches(input);
    
        // loop through the tags and build up the output string
        var builder = new StringBuilder();
        int lastIndex = 0;
        int nestingLevel = 0;
        foreach(var match in matches)
        {
            // append everything since the last tag;
            builder.Append(input.Substring(lastIndex, (match.Index - lastIndex) + 1));
    
            switch(match.Value)
            {
                case "[item]":
                    nestingLevel++;
                    builder.Append(match.Value);
                    break;
                case "[/item]":
                    nestingLevel--;
                    builder.Append(match.Value);
                    break;
                case "[position]":
                    // Append the replacement text if we're outside of any [item]/[/item] pairs
                    // Otherwise append the tag
                    builder.Append(nestingLevel == 0 ? replacement : match.Value);
                    break;
            }
            lastIndex = match.Index + match.Length;
        }
    
        builder.Append(input.Substring(lastIndex));
        return builder.ToString();
    }
    

    (Disclaimer: Have not tested. Or even attempted to compile. Apologies in advance for inevitable bugs.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to replace some value in given text using c# preferably using regex
Given the following HTML: <!doctype html> <html> <body> <span style=background-color: blue;> Some text here
Given a string with attribute/value pairs such as attr1=some text attr2 = some other
Hey guys, given a data set in plain text such as the following: ==Events==
Given a text string containing a type name, is there some way to get
Given a class such as class MyClass: text = hello number = 123 Is
I want to select blocks of text within given string. These blocks of text
Problem* Given some data (text) which has style applied to it with a loosely
Given variable ${text} , which represents a large block of text such as ...
I'm sure I'm going to lose some rep points for this, because it's such

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.