473,382 Members | 1,689 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,382 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 2267
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))...
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.