11

I am trying to use regular expressions to do some work on strings but I am having some difficulty. My goal is to replace numbers in a string with a character, specifically if there is a group of numbers in the string I want to replace the entire group of numbers with a *. If there is just a single digit I want to replace that with a ?.

For example, if I had the string "test12345.txt" I would like to turn that to "test*.txt", but if I have "test1.txt" I would like to turn that to just "test?.txt".

I tried

Regex r = new Regex(@"\d+", RegexOptions.None);
returnString = r.Replace(returnString, "*");

But this replaces replaces even a single digit on its own with a *

2
  • Regex r = new Regex(@"\d+", RegexOptions.None); returnString = r.Replace(returnString, "*"); But this replaces replaces even a single digit on its own with a * Commented Dec 21, 2011 at 8:36
  • @Jim, you have to use two regexes for your problem, see below Commented Dec 21, 2011 at 8:38

5 Answers 5

24

This is pretty easy with Regex.Replace

string input = "test12345.txt";

// replace all numbers with a single *
string replacedstar = Regex.Replace( input, "[0-9]{2,}", "*" );

// replace remaining single digits with ?
string replacedqm = Regex.Replace( input, "[0-9]", "?" );
Sign up to request clarification or add additional context in comments.

2 Comments

Is there a way to replace this all numbers with * at each position ? eg. input: test1234 results in: test**** Thanks in advance
[0-9] does not cover other languages, \d is recommended for global contexts since it checks for other languages' digits too
4

This will do, first it will match more than two digits and replace the complete block with * and the 2nd statement is for if there's single digit, it will replace with ?'

var newFileName = Regex.Replace(fileName, @"\d{2,}", "*");
newFileName = Regex.Replace(fileName, @"\d", "?");

Hope this helps.

Comments

2

Do this with two regexes:

  • replace \d{2,} with *,
  • replace \d with ?.

Comments

2
    static string ReplaceNumbers(string text)
    {
        string output = Regex.Replace(text, @"\d{2,}", "*");
        output = Regex.Replace(output, @"\d", "?");
        return output;
    }
  • \d is for digit
  • {2,} means at least 2 digits with no max limit

2 Comments

Does \d{1,1} ensure that there is only one digit? Or would it still match "2" from the string "23"? If not, what's the difference between \d{1,1} and just \d ?
@jb there is no difference between \d{1,1} and \d
0
        int n1 = Convert.ToInt32(number.Text);
        int n2 = Convert.ToInt32(replace.Text);
        int n3 = Convert.ToInt32(othernumber.Text);
        int temp;
        int result = 0;
        int i = 1;
        while (n1 > 0)
        {
            temp = n1 % 10;
            if (n2 == temp)
            {
                result += n3 * i;
            }
            else
            {
                result = result + temp * i;
            }
            i *= 10;
            n1 = n1 / 10;
        }
        afterswap.Text = Convert.ToString(result);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.