7

OLD:

private string Check_long(string input)
{
    input = input.Replace("cool", "supercool");
    input = input.Replace("cool1", "supercool1");
    input = input.Replace("cool2", "supercool2");
    input = input.Replace("cool3", "supercool3");
    return input;
}

NEW:

private string Check_short(string input)
{    
    input = Regex.Replace(input, "cool", "supercool", RegexOptions.IgnoreCase);
    input = Regex.Replace(input, "cool1", "supercool1", RegexOptions.IgnoreCase);
    input = Regex.Replace(input, "cool2", "supercool2", RegexOptions.IgnoreCase);
    input = Regex.Replace(input, "cool3", "supercool3", RegexOptions.IgnoreCase);
    return input;
}

The old solution with String.Replace was working just fine. But it didn't support case-insensitivity. So I had to check for Regex.Replace, but now it won't work. Why is that ?

5
  • 1
    The method names are different...? Commented Dec 5, 2010 at 22:28
  • 4
    What do you mean by "won't work"? Are we supposed to guess what the problem is? Commented Dec 5, 2010 at 22:30
  • By 'it won't work', I mean that the string doesn't get replaced. What I used to do is check a user's input and in case it contained "cool", "cool1" etc., I replaced the text. Now that I've changed it to Regex, it doesn't replace the user's input. Could the problem be that I'm trying to run multiple replacements on the same input string ? Commented Dec 5, 2010 at 22:49
  • Could it be a problem that I it's actually not just a word, but "cool. " or " cool. " ? Commented Dec 5, 2010 at 22:51
  • I've written a quick example of my version of your method in action - see ideone.com/Pbywh - works fine and replaces the text as expected. Commented Dec 5, 2010 at 23:02

3 Answers 3

13

Your new code should work fine. Note that you can also retain the case of your input using a capture group:

private string Check_short(string input)
{    
    return Regex.Replace(input, "(cool)", "super$1", RegexOptions.IgnoreCase);
}
Sign up to request clarification or add additional context in comments.

3 Comments

@dll32 - only if you want to capture it. The regex "cool" would also work perfectly fine, you just wouldn't have access to $1 in the replacement string..
Can you please take a quick look at ideone.com/dv4aL | I want "Mm" to be replaced my "mark" and not by "peter". It somehow seems to interpretate "MM" the same as "M ". Well seems like Regex is more complex than I thought....
If you switch your two replace statements it'll work as you intend - the problem is that your first replace "(M )" matches both statements, because they both have an M character followed by a space character.
5

working fine here:

        string input = "iiii9";
        input = Regex.Replace(input, "IIII[0-9]", "jjjj" , RegexOptions.IgnoreCase);
        label1.Text = input;

output

jjjj

Comments

-2

Regex do not work the say way that string.replace does. You need to build the regex around what you are trying to filter for.

private string Check_short(string input)
{    
    input = Regex.Replace(input, ".*(cool).*", "supercool", RegexOptions.IgnoreCase);
    return input;
}

2 Comments

Actually, "cool" is a valid regex that simply matches the string cool.
your regex will replace all text in the input string with the text supercool if input contains cool, not just the cool bit.. It'll work better without the .*s

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.