Cannot replace parenthesis using regex.replace?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

I'm trying to replace parenthesis using Regex.replace but I'm always having
this error:

System.ArgumentException: parsing ":-)" - Too many )'s. Parameter name: :-)

Here's my code:

Regex.Replace(input,":-)","img",RegexOptions.Compiled |
RegexOptions.IgnoreCase);

Any idea why? Any solution?

Thanks

Steph
 
Parentheses have special meaning in a regex; use a backslash \) in front to
match on a literal parenthesis
 
Stephane said:
I'm trying to replace parenthesis using Regex.replace but I'm always having
this error:

System.ArgumentException: parsing ":-)" - Too many )'s. Parameter name: :-)

Here's my code:

Regex.Replace(input,":-)","img",RegexOptions.Compiled |
RegexOptions.IgnoreCase);

Kyle has answered your question - but do you actually have any need to
use regular expressions in the first place? Why not just use

input.Replace (":-)", "img");

If you're trying to replace one string with another, without using any
of the special features of regular expressions, it's a lot simpler to
use String.Replace than Regex.Replace.
 
I thought using Regex.replace would be much faster than input.replace.

Is there a real performance difference between the two or I just wasted 3
hours trying to make it worked? :-D

Thanks!

Steph
 
Stephane,

StringBuilder.Replace is probably the fastest in most situations.

Cor
 
Stephane said:
I thought using Regex.replace would be much faster than input.replace.

Is there a real performance difference between the two or I just wasted 3
hours trying to make it worked? :-D

String.Replace is probably faster anyway, as it has less work to do.

However, the first thing you should always find out before using a more
complex solution is whether this is a performance bottleneck in the
first place. Have you benchmarked your app to prove you've got a
problem?
 
I haven't tested my application in a production environment, but I expect a
lot of work since this is to replace about 25 smileys code in each message
sent to my forum.

By lot of work, I mean something between 10K and 20K messages posted each day.

I'll try the string.replace and I guess I'll soon see if there's a problem
with it.

Thanks

Steph
 
Stephane,

Try it than with stringbuilder.replace as well.

A stringbuilder object is a concatenation of characters and therefore easier
to change. (Technical, logical a string is the same however technical not)

You probably will be surprised.

Cor
 
Stephane said:
I haven't tested my application in a production environment, but I expect a
lot of work since this is to replace about 25 smileys code in each message
sent to my forum.

By lot of work, I mean something between 10K and 20K messages posted each day.

That's not a lot of work at all.

Try running the following:

using System.Text;
using System;

public class Test
{
const int Iterations = 100000;

static void Main()
{
StringBuilder builder = new StringBuilder();
for (int i=0; i < 25; i++)
{
builder.Append ("before");
builder.Append (":-)");
builder.Append ("after");
}
string original = builder.ToString();

DateTime start = DateTime.Now;
for (int i=0; i < Iterations; i++)
{
string replaced = original.Replace (":-)", "img");
}
DateTime end = DateTime.Now;
Console.WriteLine ("{0} iterations took {1}", Iterations,
end-start);
}
}

On my laptop, it manages 100,000 iterations in about half a second.
Even if regular expressions were significantly faster, what's half a
second per day, really?

The moral is to always find out if something is actually going to be
expensive in real terms before going for a more complicated (and
potentially buggy) solution.
 
Cor Ligthert said:
Try it than with stringbuilder.replace as well.

A stringbuilder object is a concatenation of characters and therefore easier
to change. (Technical, logical a string is the same however technical not)

You probably will be surprised.

As with most StringBuilder operations, it depends on whether you have
any other operations to do. If you're doing lots of operations, using
StringBuilder and then converting to a string only at the end will be
faster.

If, however, you just need to do a single modification, then it's
likely to be slower to do:

StringBuilder sb = new StringBuilder(original);
sb.Replace (from, to);
string replaced = sb.ToString();

than

string replaced = original.Replace(from, to);

Also I'd say that the latter is more readable - so unless the
performance is really significant here (which it sounds like it isn't,
despite the original fears), I'd go with the simple form.
 
From that point of view, you're right! I'll go with the simple string.replace.

Thanks a lot for your help guys!

Steph
 
Jon,

My opinion as well in past, however to many argumenting with Jay where he
forever has won about this let me change my opinion.

:-)

Cor
 
Cor Ligthert said:
My opinion as well in past, however to many argumenting with Jay where he
forever has won about this let me change my opinion.

How, out of interest - and which bit?

I favour code simplicity over code efficiency until:

1) I know that the efficiency of the simple solution is a significant
problem

2) I know that the efficiency of the complex solution is significantly
better than the efficiency of the simple solution

3) I know that the complex solution will work as reliably as the simple
one (that's where unit tests come in :)

Don't get me wrong - I use StringBuilder in loops wherever there could
be significant or unbounded numbers of operations, or in particularly
performance critical sections of code. I just don't want to make the
code *any* harder to read unless there's a tangible benefit. Half a
second a day isn't worth adding code complexity for :)
 
Jon,

You will not believe it, you have the same opinon about this as me.

However in some tests the performance from the stringbuilder was forever
higher, I did not test this one now, however I think that the times that a
part that has to be changed in the string can be of influence of this.
(Although it is not impossible that this is as well optimized and that for
this the string.replace uses internally the stringbuilder.replace).

I have some other things to do today that needs my attention, therefore I
have probably no time to test what I wrote above, maybe in the weekend when
I think about it.

Cor
 
Cor Ligthert said:
You will not believe it, you have the same opinon about this as me.
:)

However in some tests the performance from the stringbuilder was forever
higher, I did not test this one now, however I think that the times that a
part that has to be changed in the string can be of influence of this.
(Although it is not impossible that this is as well optimized and that for
this the string.replace uses internally the stringbuilder.replace).

I have some other things to do today that needs my attention, therefore I
have probably no time to test what I wrote above, maybe in the weekend when
I think about it.

I've just tested it by taking the program I posted earlier, and
changing the bit inside the loop to:

StringBuilder builder2 = new StringBuilder (original);
builder2.Replace(":-)", "img");
string replaced = builder2.ToString();

It took very slightly longer - over 1,000,000 iterations,
String.Replace took 5.78s, whereas the above took 5.86s.

As I said, if you were to do more than one replace operation,
StringBuilder would be faster - but when the performance isn't a
bottleneck, I'd still stick to the simpler code.
 
Back
Top