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

  • Home
  • SEARCH
  • 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 8786869
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T21:44:40+00:00 2026-06-13T21:44:40+00:00

Am I understanding how RegEx.Replace works in C#/.NET? I want this: Test: String To

  • 0

Am I understanding how RegEx.Replace works in C#/.NET? I want this:

Test: String

To become this:

Test String

var cleanString = Regex.Replace("Test: String", @"^[\w\*\$][\w\s\-\$]*(\(\d{1,}\)){0,1}$", "");

Yet it cleanString evaluates to:

Test: String

What am I doing wrong?

EDIT: I’m getting a regex validation string from a third party source, so I can either use that regex validation string or somehow figure out the valid characters in the regex validation string, and loop through an invalid character array. This is why I chose to do regex, which I never usually use.

  • 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-13T21:44:41+00:00Added an answer on June 13, 2026 at 9:44 pm

    Regex.Replace won’t do what you’re trying to do. The regex pattern used in Regex.Replace needs to match on the bits of the string that you want to replace.

    The string validation pattern you’re trying to use matches only a valid string. You can use Regex.IsMatch with this pattern to check whether or not a string is valid, but it won’t, and can’t, show you which characters in the string prevent the match from succeeding.

    You need to use a pattern that matches bad characters. A first stab for this situation would be [^\w\*\-\d\(\)\s\$]+. This will match any character not allowed by the validation string, but is still not good enough. For example, it won’t match non-digit characters between the optional parentheses, and it won’t enforce a single pair of matching parentheses. When you start to consider all the possible ways a string could be invalid as single Regex pattern to strip out invalid characters will quickly become unworkable.

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

Sidebar

Related Questions

I'm having a hard time understanding this regex stuff... I have a string like
I am working in VB.Net and trying to use Regex.Replace to format a string
I just need someone to correct my understanding of this regex , which is
I want to remove 'www.' and '.com' from this string 'www.example.com' I want to
I'm looking over some code and found the following regex: var querystring = querystring.replace(/[^&]+=\.?(?:&|$)/g,
My understanding of regex and working with strings in php is poor at best
I have the following regex: (?<=\.\d+?)0+(?=\D|$) I'm running it against a string which contains
I have the following regex in a C# program, and have difficulties understanding it:
Using Regex in .Net I will have a set of data that comes in
What would be the equivalent of this (Javascript) in python? var patt = /sub(\d+)\.domain\.com\/(\d+)/

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.