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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T08:19:10+00:00 2026-05-20T08:19:10+00:00

Problem I need to replace all asterisk symbols(‘*’) with percent symbol(‘%’). The asterisk symbols

  • 0

Problem

I need to replace all asterisk symbols(‘*’) with percent symbol(‘%’). The asterisk symbols in square brackets should be ignored.

Example

    [Test]
    public void Replace_all_asterisks_outside_the_square_brackets()
    {
        var input = "Hel[*o], w*rld!";
        var output = Regex.Replace(input, "What_pattern_should_be_there?", "%")

        Assert.AreEqual("Hel[*o], w%rld!", output));
    }
  • 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-05-20T08:19:11+00:00Added an answer on May 20, 2026 at 8:19 am

    Try using a look ahead:

    \*(?![^\[\]]*\])
    

    Here’s a bit stronger solution, which takes care of [] blocks better, and even escaped \[ characters:

    string text = @"h*H\[el[*o], w*rl\]d!";
    string pattern = @"
    \\.                 # Match an escaped character. (to skip over it)
    |
    \[                  # Match a character class 
        (?:\\.|[^\]])*  # which may also contain escaped characters (to skip over it)
    \]
    |
    (?<Asterisk>\*)     # Match `*` and add it to a group.
    ";
    
    text = Regex.Replace(text, pattern,
        match => match.Groups["Asterisk"].Success ? "%" : match.Value,
        RegexOptions.IgnorePatternWhitespace);
    

    If you don’t care about escaped characters you can simplify it to:

    \[          # Skip a character class
        [^\]]*  # until the first ']'
    \]
    |
    (?<Asterisk>\*)
    

    Which can be written without comments as: @"\[[^\]]*\]|(?<Asterisk>\*)".

    To understand why it works we need to understand how Regex.Replace works: for every position in the string it tries to match the regex. If it fails, it moves one character. If it succeeds, it moves over the whole match.
    Here, we have dummy matches for the [...] blocks so we may skip over the asterisks we don’t want to replace, and match only the lonely ones. That decision is made in a callback function that checks if Asterisk was matched or not.

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

Sidebar

Related Questions

I have a problem with the symbols ^ and . I need to replace
With Regex, I need to find and replace all the mailaddresses in a fully
I need to search a string and replace all occurrences of %FirstName% and %PolicyAmount%
The title says all. i need to replace a property value whom i don't
I need to replace all the matches of a regular expression till the caret
I have a string and I need to replace all the &#39; and etc
I need a some help to replace all non-word characters in a string. As
Goal: Gain datatype date with year and month Problem: Need to help to convert
Problem: I need to write/read objects from a file.This because I need to write/read
The problem: I need to ensure that a particular directory hierarchy exists in the

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.