473,738 Members | 11,146 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Cannot replace parenthesis using regex.replace?

Hi,

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

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

Here's my code:

Regex.Replace(i nput,":-)","img",RegexO ptions.Compiled |
RegexOptions.Ig noreCase);

Any idea why? Any solution?

Thanks

Steph
Oct 12 '05 #1
16 9072
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******@discu ssions.microsof t.com> wrote in message
news:A6******** *************** ***********@mic rosoft.com...
Hi,

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

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

Here's my code:

Regex.Replace(i nput,":-)","img",RegexO ptions.Compiled |
RegexOptions.Ig noreCase);

Any idea why? Any solution?

Thanks

Steph

Oct 12 '05 #2
Stephane <St******@discu ssions.microsof t.com> wrote:
I'm trying to replace parenthesis using Regex.replace but I'm always having
this error:

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

Here's my code:

Regex.Replace(i nput,":-)","img",RegexO ptions.Compiled |
RegexOptions.Ig noreCase);


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.co m>
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******@discu ssions.microsof t.com> wrote:
I'm trying to replace parenthesis using Regex.replace but I'm always having
this error:

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

Here's my code:

Regex.Replace(i nput,":-)","img",RegexO ptions.Compiled |
RegexOptions.Ig noreCase);


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.co m>
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.R eplace is probably the fastest in most situations.

Cor
Oct 12 '05 #5
Stephane <St******@discu ssions.microsof t.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.co m>
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******@discu ssions.microsof t.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.co m>
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******@discu ssions.microsof t.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.co m>
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.r eplace 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******@discu ssions.microsof t.com> schreef in bericht
news:08******** *************** ***********@mic rosoft.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******@discu ssions.microsof t.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.co m>
> 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******@discu ssions.microsof t.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.ToStrin g();

DateTime start = DateTime.Now;
for (int i=0; i < Iterations; i++)
{
string replaced = original.Replac e (":-)", "img");
}
DateTime end = DateTime.Now;
Console.WriteLi ne ("{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.co m>
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

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

Similar topics

1
12652
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 character. Can anyone get this to work? Thanks
38
15963
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 something built-in that can accomplish this, or do I have to write my own parser? I don't want to reinvent the wheel if possible.
6
8883
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 Regex.Replace("<stuff>text</stuff>", "<stuff>", "<stuff>\n" would give yo <stuff text</stuff
16
1215
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: Regex.Replace(input,":-)","img",RegexOptions.Compiled |
1
1843
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( rest, "(https?:/{0,2}.+)$", "$1", RegexOptions.IgnoreCase ); My expectation is that in redir_url it would contain http://www.someurl.com.
2
2415
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, in compile return _compile(pattern, flags) File "C:\Python25\lib\re.py", line 233, in _compile
11
2955
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 new car is <em>red</em>! Please don't think to be in Nascar!!"
0
8969
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8788
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9476
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9263
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9208
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8210
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6053
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4825
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3279
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.