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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:17:47+00:00 2026-06-15T09:17:47+00:00

Part of an app I’m creating in C# replaces certain substrings in a string

  • 0

Part of an app I’m creating in C# replaces certain substrings in a string with a value in square brackets like [11]. Often there can be the same value straight after – so I want to reduce the amount of text by combining them into one like [11,numberOfSame]

For example, if the string contains:
blahblah[122][122][122]blahblahblahblah[18][18][18][18]blahblahblah

The desired new string would be:
blahblah[122,3]blahblahblahblah[18,4]blahblahblah

Would anyone know how I would do this? Thanks! 🙂

  • 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-15T09:17:47+00:00Added an answer on June 15, 2026 at 9:17 am
    Regex.Replace("blahblah[122][122][122]blahblahblahblah[18][18][18][18]blahblahblah",
        @"(\[([^]]+)])(\1)+",
        m => "[" + m.Groups[2].Value + "," + (m.Groups[3].Captures.Count + 1) + "]")
    

    Returns:

    blahblah[122,3]blahblahblahblah[18,4]blahblahblah
    

    Explanation of regex:

    (           Starts group 1
      \[        Matches [
      (         Starts group 2
        [^]]+   Matches 1 or more of anything but ]
      )         Ends group 2
      ]         Matches ]
    )           Ends group 1
    (           Starts group 3
      \1        Matches whatever was in group 1
    )           Ends group 3
    +           Matches one or more of group 3
    

    Explanation of lambda:

    m =>                                Accepts a Match object
    "[" +                               A [
    m.Groups[2].Value +                 Whatever was in group 2
    "," +                               A ,
    (m.Groups[3].Captures.Count + 1) +  The number of times group 3 matched + 1
    "]"                                 A ]
    

    I am using this overload, which accepts a delegate to compute the replacement value.

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

Sidebar

Related Questions

I've been developing my first iPhone app part-time and would like to start using
I have a part of my app that takes a screenshot of a certain
I am tyring to accomplish a part of my app where the user can
Reinstalling a Rails app on a new server. Part of the app can fork
As part of our app, user can save some data as XML on server
As part of my app, users get to approve certain requests via email. Requests
I've got a part in my app where the user can view some notes
Part of my app caches web pages for offline viewing. To do that, I
Part of an app I am working on includes a log file viewer, with
This part of an app that I am working on, I have the following

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.