PC Review


Reply
Thread Tools Rate Thread

Cannot replace parenthesis using regex.replace?

 
 
=?Utf-8?B?U3RlcGhhbmU=?=
Guest
Posts: n/a
 
      12th Oct 2005
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
 
Reply With Quote
 
 
 
 
Kyle Alons
Guest
Posts: n/a
 
      12th Oct 2005
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" <(E-Mail Removed)> wrote in message
news:A67FC9A7-2B3F-4CDD-9398-(E-Mail Removed)...
> 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



 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      12th Oct 2005
Stephane <(E-Mail Removed)> 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 - <(E-Mail Removed)>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
=?Utf-8?B?U3RlcGhhbmU=?=
Guest
Posts: n/a
 
      12th Oct 2005
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 <(E-Mail Removed)> 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 - <(E-Mail Removed)>
> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
> If replying to the group, please do not mail me too
>

 
Reply With Quote
 
Cor Ligthert [MVP]
Guest
Posts: n/a
 
      12th Oct 2005
Stephane,

StringBuilder.Replace is probably the fastest in most situations.

Cor


 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      12th Oct 2005
Stephane <(E-Mail Removed)> 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 - <(E-Mail Removed)>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
=?Utf-8?B?U3RlcGhhbmU=?=
Guest
Posts: n/a
 
      12th Oct 2005
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 <(E-Mail Removed)> 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 - <(E-Mail Removed)>
> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
> If replying to the group, please do not mail me too
>

 
Reply With Quote
 
=?Utf-8?B?U3RlcGhhbmU=?=
Guest
Posts: n/a
 
      12th Oct 2005
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 <(E-Mail Removed)> 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 - <(E-Mail Removed)>
> > http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
> > If replying to the group, please do not mail me too
> >

 
Reply With Quote
 
Cor Ligthert [MVP]
Guest
Posts: n/a
 
      12th Oct 2005
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" <(E-Mail Removed)> schreef in bericht
news:08005927-688B-4EC0-B830-(E-Mail Removed)...
> 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 <(E-Mail Removed)> 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 - <(E-Mail Removed)>
>> > http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
>> > If replying to the group, please do not mail me too
>> >



 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      12th Oct 2005
Stephane <(E-Mail Removed)> 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 - <(E-Mail Removed)>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How do i find and replace words in parenthesis? =?Utf-8?B?Qm9iIGluIEFyaXpvbmE=?= Microsoft Word Document Management 5 1st Sep 2006 06:31 AM
Use regex.replace to replace a group #, not the whole match string justin.mayes@gmail.com Microsoft VB .NET 1 2nd Jun 2006 11:07 PM
RegEx.Replace replace more than one kids_pro Microsoft C# .NET 3 10th Aug 2004 09:19 AM
Performance Regex Replace vs StringBuilder Replace Jay B. Harlow [MVP - Outlook] Microsoft VB .NET 8 22nd Mar 2004 01:08 AM
Replace methode, Replace Function, Stringbuilder replace, Regex Replace, Split Cor Microsoft VB .NET 4 1st Mar 2004 02:50 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:28 AM.