Out of curiosity, I created a small test program. The results were somewhat expected (Regular Expression matching is slower), but there are a number of factors to consider.
First, Regular Expressions are VERY powerful. Besides doing matching, they can do validation. Also, you can create very powerful expressions much easier than you could with IndexOf and Substring.
I'll make two notes about the sample code. First, I wrote the regular expression code in about 5 minutes. Writing the IndexOf and Substring took about 15 minutes. Also, my first regular expression is MUCH more robust than the IndexOf/Substring method. For instance, the expression will automatically trim off any spaces or whitespace along with weird characters.
Also, the code for regular expressions is MUCH more readable since each match is named. To get the last name, I simply use:
C#:
lastName = match.Groups["LastName"].Value;
Using IndexOf, I had to use:
C#:
= 2;
2 = smallData.IndexOf(' ', +1);
lastName = smallData.Substring( +1, 2 - - 1);
Without comments, it's hard to say what's going on. Which code would you rather look at a year from now?
Having said that, the speed is really dependent on what you need to do. If you need to parse through a 4 gig text file, I'd go with the fastest method possible and hard-code as many settings as possible. If you're parsing a string or two, I'd go with whatever is easier to maintain as both Regular Expressions and IndexOf are going to be
perceptibly the same to the user.
Here are the results after running the project in Debug mode in the IDE on my machine:
Code:
short Data RegEx: 828
large Data RegEx: 2578
short Data Substring: 31
large Data Substring: 94
Press ENTER to close
Keep in mind this is for 100,000 iterations. For 1000 iterations, all 4 tests come in at 0ms on my machine.
-Nerseus