472,958 Members | 2,563 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Are Regex slower than methods from classes like String & Char?

Hi

I wonder if regular expressions are in general sower than using
classes like String and
Char when used for validating/parsing text data?

I've done some simple test (using IsMatch()) method and the result was
that Regex
is either as fast or two times slower than method which used methods
from classes
String and Char.
(I tried static and object method, I also tried Compiled option)

Maybe Regex are slower only when matching is relatively simple?

Regards

May 23 '07 #1
3 2238
om****@wp.pl wrote:
Hi

I wonder if regular expressions are in general sower than using
classes like String and
Char when used for validating/parsing text data?

I've done some simple test (using IsMatch()) method and the result was
that Regex
is either as fast or two times slower than method which used methods
from classes
String and Char.
(I tried static and object method, I also tried Compiled option)

Maybe Regex are slower only when matching is relatively simple?
For simple patterns, Regex is probably slower. For complex matches, regex
is more likely to be faster.

-cd
May 23 '07 #2
* om****@wp.pl wrote, On 23-5-2007 15:27:
Hi

I wonder if regular expressions are in general sower than using
classes like String and
Char when used for validating/parsing text data?

I've done some simple test (using IsMatch()) method and the result was
that Regex
is either as fast or two times slower than method which used methods
from classes
String and Char.
(I tried static and object method, I also tried Compiled option)

Maybe Regex are slower only when matching is relatively simple?
There's a discussion a couple of threads back. The outcome was quite
logical. You need to balance, maintainability, performance, suitability
between the follwoing three methods

Simple patterns (startswith, endswith, length, contains fixed text...)
- Use string based functions. For shorter strings direct manipulation is
faster, for longer strings stringbuilder is better.

Complexer patterns (contains variable strings, matches a complex
pattern, extensive search and replace option, large optional parts etc)
- Use a Regular expression. If the expression is going to be reused
multiple time, initialize the expression to a Regex object and assing
that to a static field. Use the RegexOptions.Compiled to convert the
expression to IL. For adhoc or variable expression (maybe built from
user input) don't use RegexOptions.Compiled, but use the static methods
from the Regex class instead.
Make use of the option to include whitespace and comments in your regex.
Set the RegexOptions.IgnorePatternWhitespace option. Then format your
expressions as follows:
@"
(
A (?# Documentation for alternative A)
| B (?# Documentation for alternative B)
| C (?# Documentation for alternative C)
)+
";
If you need to match a space character you'll have to put it in a
character class like this: "[ ]".

Quite complex problems (resulting in unreadable regexes, hard to explain
problems etc)
Split the problem in sub-problems. Apply the above rules to use the
appropriate method to solve these sub problems. The same as you would do
with too complex methods.

Very complex problems (anything which results in a hard to maintain code
with lots of subroutines and regular expressions and string manipulation).
Use a scanner/parser generator. There's one you can get with the Visual
Studio 2005 SDK which generates C# code based on your template.

Performance wise, always test your assertions. If performance isn't that
much of a problem, err to the more maintainable version.

Jesse Houwing
May 23 '07 #3
Hi,

<om****@wp.plwrote in message
news:11**********************@q69g2000hsb.googlegr oups.com...
Hi

I wonder if regular expressions are in general sower than using
classes like String and
Char when used for validating/parsing text data?

I've done some simple test (using IsMatch()) method and the result was
that Regex
is either as fast or two times slower than method which used methods
from classes
String and Char.
(I tried static and object method, I also tried Compiled option)

Maybe Regex are slower only when matching is relatively simple?
That's correct.

The good thing about Regex is when you have something else but trivial
pattern, the expressivity you get with the Regex is imposible to get using
other means.
May 23 '07 #4

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

Similar topics

7
by: alphatan | last post by:
Is there relative source or document for this purpose? I've searched the index of "Mastering Regular Expression", but cannot get the useful information for C. Thanks in advanced. -- Learning...
9
by: Tim Conner | last post by:
Is there a way to write a faster function ? public static bool IsNumber( char Value ) { if (Regex.IsMatch( Value.ToString(), @"^+$" )) { return true; } else return false; }
7
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)...
17
by: clintonG | last post by:
I'm using an .aspx tool I found at but as nice as the interface is I think I need to consider using others. Some can generate C# I understand. Your preferences please... <%= Clinton Gallagher ...
4
by: Brian Henry | last post by:
I have phone numbers like this in a data table 123-435-1234 1231231234 432.234.2321 they all have different formatting, what I want to do is get them all formatted like this (123) 123-1234
11
by: Steve | last post by:
Hi All, I'm having a tough time converting the following regex.compile patterns into the new re.compile format. There is also a differences in the regsub.sub() vs. re.sub() Could anyone lend...
10
by: igor.kulkin | last post by:
I have a small utility program written in Python which works pretty slow so I've decided to implement it in C. I did some benchmarking of Python's code performance. One of the parts of the program...
15
by: morleyc | last post by:
Hi, i would like to remove a number of characters from my string (\t \r \n which are throughout the string), i know regex can do this but i have no idea how. Any pointers much appreciated. Chris
7
by: =?Utf-8?B?amFj?= | last post by:
Hi, I have problems with following code and don’t find the bug : // Set ArrayList aArray = new ArrayList(); regStr = new Regex(@"\?)*(\d+)\]"); if(text != null && regStr.IsMatch(text))...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.