473,761 Members | 2,455 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to make Regex.Replace faster?

In my case, I have to remove any line containing "0.000000" from input
string.
In below case, it takes about 100 ms for 2k size input string.
Regex.Replace(i nputString, ".*0\\.000000.* \n", "");
I want to optimize it, so i make a static member instance instead of
using static func of Regex;
static Regex filter= new Regex(".*0\\.00 0000.*\n",
RegexOptions.Co mpiled);
And use the static instance to replace the string.
filter.Replace( inputString, "");
Now, it takes about 200 ms. The platform is .net 1.1.
I don't understand why the non-static member function is slower thant
static one. The static function has to compile the regular expression
each time, right?

Is there any better solution to remove substring of specific pattern?
Perhaps I have to write my code to do it.

Thanks,
-Morgan

Sep 14 '06 #1
4 3147
Morgan,

To make regex faster try to avoid it by using a replace or an other method.

Regex is approx. 100 times slower in simple replacements than by instance
"replace".
(This will be gained again in complex situation where you would have to do a
lot of replacements of course)

I hope this helps,

Cor
"Morgan Cheng" <mo************ @gmail.comschre ef in bericht
news:11******** ************@k7 0g2000cwa.googl egroups.com...
In my case, I have to remove any line containing "0.000000" from input
string.
In below case, it takes about 100 ms for 2k size input string.
Regex.Replace(i nputString, ".*0\\.000000.* \n", "");
I want to optimize it, so i make a static member instance instead of
using static func of Regex;
static Regex filter= new Regex(".*0\\.00 0000.*\n",
RegexOptions.Co mpiled);
And use the static instance to replace the string.
filter.Replace( inputString, "");
Now, it takes about 200 ms. The platform is .net 1.1.
I don't understand why the non-static member function is slower thant
static one. The static function has to compile the regular expression
each time, right?

Is there any better solution to remove substring of specific pattern?
Perhaps I have to write my code to do it.

Thanks,
-Morgan

Sep 15 '06 #2
Morgan Cheng <mo************ @gmail.comwrote :
In my case, I have to remove any line containing "0.000000" from input
string.
In below case, it takes about 100 ms for 2k size input string.
Regex.Replace(i nputString, ".*0\\.000000.* \n", "");
I want to optimize it, so i make a static member instance instead of
using static func of Regex;
static Regex filter= new Regex(".*0\\.00 0000.*\n",
RegexOptions.Co mpiled);
And use the static instance to replace the string.
filter.Replace( inputString, "");
Now, it takes about 200 ms. The platform is .net 1.1.
I don't understand why the non-static member function is slower thant
static one. The static function has to compile the regular expression
each time, right?

Is there any better solution to remove substring of specific pattern?
Perhaps I have to write my code to do it.
I would try using a StringReader, read the data line by line, and
append any line which doesn't contain 0.000000 - simple string
operations. If you could provide a test program you've used to gather
your performance data, we could experiment a bit.

--
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
Sep 15 '06 #3

Cor Ligthert [MVP] 写道:
Morgan,

To make regex faster try to avoid it by using a replace or an other method.
Do you mean it is not recommeded to trim string with Regex?
So, the best way for is to code manually handling the string?
Regex is approx. 100 times slower in simple replacements than by instance
"replace".
(This will be gained again in complex situation where you would have to do a
lot of replacements of course)

I hope this helps,

Cor
"Morgan Cheng" <mo************ @gmail.comschre ef in bericht
news:11******** ************@k7 0g2000cwa.googl egroups.com...
In my case, I have to remove any line containing "0.000000" from input
string.
In below case, it takes about 100 ms for 2k size input string.
Regex.Replace(i nputString, ".*0\\.000000.* \n", "");
I want to optimize it, so i make a static member instance instead of
using static func of Regex;
static Regex filter= new Regex(".*0\\.00 0000.*\n",
RegexOptions.Co mpiled);
And use the static instance to replace the string.
filter.Replace( inputString, "");
Now, it takes about 200 ms. The platform is .net 1.1.
I don't understand why the non-static member function is slower thant
static one. The static function has to compile the regular expression
each time, right?

Is there any better solution to remove substring of specific pattern?
Perhaps I have to write my code to do it.

Thanks,
-Morgan
Sep 15 '06 #4
Morgan,
>Do you mean it is not recommeded to trim string with Regex?
So, the best way for is to code manually handling the string?
If you can avoid Regex try that, but not in the case that you have to done a
lot of things for it, or make your code not readable for others anymore.
Than use Regex.

(And than you probably on the place that it performs in at least the same).

Just my opinion.

Cor
"Morgan Cheng" <mo************ @gmail.comschre ef in bericht
news:11******** **************@ b28g2000cwb.goo glegroups.com.. .

Cor Ligthert [MVP] ??:
Morgan,

To make regex faster try to avoid it by using a replace or an other
method.
Do you mean it is not recommeded to trim string with Regex?
So, the best way for is to code manually handling the string?
Regex is approx. 100 times slower in simple replacements than by instance
"replace".
(This will be gained again in complex situation where you would have to do
a
lot of replacements of course)

I hope this helps,

Cor
"Morgan Cheng" <mo************ @gmail.comschre ef in bericht
news:11******** ************@k7 0g2000cwa.googl egroups.com...
In my case, I have to remove any line containing "0.000000" from input
string.
In below case, it takes about 100 ms for 2k size input string.
Regex.Replace(i nputString, ".*0\\.000000.* \n", "");
I want to optimize it, so i make a static member instance instead of
using static func of Regex;
static Regex filter= new Regex(".*0\\.00 0000.*\n",
RegexOptions.Co mpiled);
And use the static instance to replace the string.
filter.Replace( inputString, "");
Now, it takes about 200 ms. The platform is .net 1.1.
I don't understand why the non-static member function is slower thant
static one. The static function has to compile the regular expression
each time, right?

Is there any better solution to remove substring of specific pattern?
Perhaps I have to write my code to do it.

Thanks,
-Morgan

Sep 15 '06 #5

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

Similar topics

3
2084
by: Jon Maz | last post by:
Hi All, Am getting frustrated trying to port the following (pretty simple) function to CSharp. The problem is that I'm lousy at Regular Expressions.... //from http://support.microsoft.com/default.aspx?scid=kb;EN-US;246800 function fxnParseIt() { var sInputString = 'asp and database';
7
2618
by: bill tie | last post by:
I'd appreciate it if you could advise. 1. How do I replace "\" (backslash) with anything? 2. Suppose I want to replace (a) every occurrence of characters "a", "b", "c", "d" with "x", (b) every occurrence of characters "p", "q", "r", "s" with "y". Right now, I do it as follows:
5
29065
by: Greg Collins [InfoPath MVP] | last post by:
I couldn't find anything in my searches... I'm wondering if there's a Regex (with or without additional C# code) that can convert a either "lowerCamelCase" or "UpperCamelCase" into a proper "Title Case" (with spaces). Thanx! -- Greg Collins Please visit: http://www.InfoPathDev.com
10
2184
by: Extremest | last post by:
I know there are ways to make this a lot faster. Any newsreader does this in seconds. I don't know how they do it and I am very new to c#. If anyone knows a faster way please let me know. All I am doing is quering the db for all the headers for a certain group and then going through them to find all the parts of each post. I only want ones that are complete. Meaning all segments for that one file posted are there. using System;
6
2503
by: Extremest | last post by:
I have a huge regex setup going on. If I don't do each one by itself instead of all in one it won't work for. Also would like to know if there is a faster way tried to use string.replace with all the right parts in there in one big line and for some reason that did not work either. Here is my regex's. static Regex rar = new Regex("\\.part.*", RegexOptions.IgnoreCase); static Regex par = new Regex("\\.vol.*", RegexOptions.IgnoreCase);
3
8502
by: jwgoerlich | last post by:
Hello group, I am working on a query string class. The purpose is to parse name-value pairs from incoming text. Currently, I am using the Regex code below. I have two questions. First, the code below does not work if there is a space in the name. For example, the text "Initial Catalog=test;" parses to name=Catalog and value=test.
6
2567
by: =?Utf-8?B?U2VyZ2V5IFBvYmVyZXpvdnNraXk=?= | last post by:
Hi, I need to replace double quotes inside the text from the database with " to correctly display my text. I was trying to use Regex to perform such a task: Regex.Replace(text, "", """) to catch standard " sign and non-standard “ and ” - but non-standard double quotes are not recognised by Replace function. Can anyone suggest the correct expression to use (I was trying to avoid multiple String.Replace functions that work fine - I...
5
9299
by: V S Rawat | last post by:
I was trying to use back-to-back replace functions to convert a url: str1 = str.replace("%2F","/").replace("%3F","?").replace("%3D","=").replace("%2 6","&"); It didn't replace all 4 types of strings. Then, I googled and found this suggestion of some JavaScript Tutorials, so I used replace with a regex with a global switch:
4
2144
by: seberino | last post by:
I'm looking over the docs for the re module and can't find how to "NOT" an entire regex. For example..... How make regex that means "contains regex#1 but NOT regex#2" ? Chris
0
9538
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, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
9353
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
10123
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...
0
9975
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8794
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 projectplanning, coding, testing, and deploymentwithout 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
6623
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
5384
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3481
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2765
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.