2

So i have some strings which I want to replace some occurrences of brackets in.

Now this is what I've done so far. And it works

string answerText = "This is an [example] string";
Match match = Regex.Match(answerText, @"\[(.*?)\]");

if(match.Success)
{
    if(match.Value.Equals("[example]"))
    {
        answerText = answerText.Replace(match.Value, "awsome");
    }
}

What I'm trying to figure out is how to make this happen if the answer text looks something like this

string answerText = "This is an [example1] [example2] [example3] string";
John Saunders
  • 160,644
  • 26
  • 247
  • 397
Eric Herlitz
  • 25,354
  • 27
  • 113
  • 157
  • 1
    More generally you might want to look at balanced regexs e.g. http://stackoverflow.com/questions/4284827/regular-expression-that-uses-balancing-groups – Ian Mercer Sep 21 '11 at 14:33
  • 1
    Surely you aren't just looking for the [Regex.Replace](http://msdn.microsoft.com/en-us/library/xwewhkd1.aspx) method? *Within a specified input string, replaces all strings that match a regular expression pattern with a specified replacement string.* – user Sep 21 '11 at 14:36
  • no nesting, I'm thinking if Regex.Matches could be an option to solve this – Eric Herlitz Sep 21 '11 at 20:57

4 Answers4

3

Is there any reason why you're not doing this instead of using regexes?

string answerText = "This is an [example] string";
answerText.Replace("[example]", "awsome");
Evren Kuzucuoglu
  • 3,781
  • 28
  • 51
  • 1
    maybe he does not know what the text between the brackets will be?? –  Sep 21 '11 at 14:37
  • 1
    @CodeMonkey, that was my thought too. The regex used supports that hypothesis. – user Sep 21 '11 at 14:38
  • Hence the question mark. The question seems to indicate he's going to test the match against actual values to see if he needs to replace them, in which case you don't need to use Regex... – Evren Kuzucuoglu Sep 21 '11 at 14:49
  • The values within the brackets can be anything so .replace is a bit to simple – Eric Herlitz Sep 21 '11 at 15:36
1

What about this

string answerText = "This is an [example1] [example2] [example3] string";
string pattern = @"\[(.*?)\]";
answerText = Regex.Replace(answerText, pattern, "awesome");
stema
  • 90,351
  • 20
  • 107
  • 135
  • Hint: The question mark makes the pattern "lazy", i.e. using the shortest match available. Combine this with the Replace-Overload with a callback function and you will be able to "switch case" over your pattern... – eFloh Sep 21 '11 at 14:46
  • Wouldn't this replace all [whatever] with "awesome" ? – Eric Herlitz Sep 21 '11 at 15:37
  • @Trikks yes thats what this is doing. – stema Sep 21 '11 at 18:13
  • Alright, I need to be able to have dynamic replacements. Thanks anyway – Eric Herlitz Sep 21 '11 at 20:56
  • @Trikks have a look at [MatchEvaluator](http://msdn.microsoft.com/query/dev10.query?appId=Dev10IDEF1&l=EN-US&k=k(SYSTEM.TEXT.REGULAREXPRESSIONS.MATCHEVALUATOR);k(MATCHEVALUATOR);k(TargetFrameworkMoniker-%22.NETFRAMEWORK%2cVERSION%3dV4.0%22);k(DevLang-CSHARP)&rd=true) this fits to the comment of eFloh and should meet your requirement of dynamical replacements. – stema Sep 21 '11 at 21:11
0

You can use Replace method of Regex something like below.

     Regex.Replace(inputString, "\\[" + matchValue + "\\]", "ReplaceText", RegexOptions.IgnoreCase);

Hope this helps!!

Praveen
  • 1,449
  • 16
  • 25
0

Solved this by using the solution I suggested in my original post looping through the string several times until I got no match.

Eric Herlitz
  • 25,354
  • 27
  • 113
  • 157