473,322 Members | 1,403 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Cannot replace parenthesis using regex.replace?

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
Oct 12 '05 #1
16 9024
Parentheses have special meaning in a regex; use a backslash \) in front to
match on a literal parenthesis

--
---------------------------------------------------
Automate your software builds with Visual Build Pro
http://www.visualbuild.com/

"Stephane" <St******@discussions.microsoft.com> wrote in message
news:A6**********************************@microsof t.com...
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

Oct 12 '05 #2
Stephane <St******@discussions.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Oct 12 '05 #3
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

"Jon Skeet [C# MVP]" wrote:
Stephane <St******@discussions.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Oct 12 '05 #4
Stephane,

StringBuilder.Replace is probably the fastest in most situations.

Cor
Oct 12 '05 #5
Stephane <St******@discussions.microsoft.com> wrote:
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?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Oct 12 '05 #6
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

"Jon Skeet [C# MVP]" wrote:
Stephane <St******@discussions.microsoft.com> wrote:
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?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Oct 12 '05 #7
For your information, here's a benchmark that tells what I want!

http://www.anothereon.net/weblog/arc...09/05/188.aspx

Conclusion: Always ask google before doing something stupid! :-D

Steph

"Stephane" wrote:
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

"Jon Skeet [C# MVP]" wrote:
Stephane <St******@discussions.microsoft.com> wrote:
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?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Oct 12 '05 #8
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" <St******@discussions.microsoft.com> schreef in bericht
news:08**********************************@microsof t.com...
For your information, here's a benchmark that tells what I want!

http://www.anothereon.net/weblog/arc...09/05/188.aspx

Conclusion: Always ask google before doing something stupid! :-D

Steph

"Stephane" wrote:
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

"Jon Skeet [C# MVP]" wrote:
> Stephane <St******@discussions.microsoft.com> wrote:
> > 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?
>
> --
> Jon Skeet - <sk***@pobox.com>
> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
> If replying to the group, please do not mail me too
>

Oct 12 '05 #9
Stephane <St******@discussions.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Oct 12 '05 #10
Cor Ligthert [MVP] <no************@planet.nl> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Oct 12 '05 #11
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 Skeet [C# MVP]" wrote:
Stephane <St******@discussions.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Oct 12 '05 #12
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
Oct 12 '05 #13
Cor Ligthert [MVP] <no************@planet.nl> wrote:
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 Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Oct 12 '05 #14
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

"Jon Skeet [C# MVP]" <sk***@pobox.com> schreef in bericht
news:MP************************@msnews.microsoft.c om...
Cor Ligthert [MVP] <no************@planet.nl> wrote:
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 Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Oct 13 '05 #15
Cor Ligthert [MVP] <no************@planet.nl> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Oct 13 '05 #16
Thanks,

Cor
Oct 13 '05 #17

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: GlennH | last post by:
I am having trouble removing a pipe character using Regex.Replace - see the 2 NUnit tests below: The first replace works fine and the second Replace does not work. I've tried escaping the pipe...
38
by: Steve Kirsch | last post by:
I need a simple function that can match the number of beginning and ending parenthesis in an expression. Here's a sample expression: ( ( "john" ) and ( "jane" ) and ( "joe" ) ) Does .NET have...
6
by: Chris Anderson | last post by:
Anyone know of a fix (ideally) or an easy workaround to the problem of escape characters not working in regex replacement text? They just come out as literal text For example, you'd think that thi...
16
by: Stephane | last post by:
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: ...
1
by: Amy L | last post by:
I must be missing something here - I am thinking this should work but it simply does not. String rest = "/?id=3&rd=httP://www.someurl.com" String redir_uri = "" ; redir_uri = Regex.Replace(...
2
by: voxiac | last post by:
Could someone tell me why: Fails with message: Traceback (most recent call last): File "<pyshell#12>", line 1, in <module> re.compile('\\dir\\(file)') File "C:\Python25\lib\re.py", line 180,...
11
by: Fabri | last post by:
I searched and tried to develop (with no luck) a function to do the following: I have a string that may be: "Le'ts go to <a href="my.htm">my car</a>. Tomorrow I'll have to buy a new car. My...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.