views:

77

answers:

3

On a single line Regex if I want to capture all till the end of line.. Which of these will be better in terms of Performance?

Regex r = new Regex(".+");

OR

Regex r = new Regex("[^$]+$");

I haven't included the entire regex but I hope you get the idea.

Are there any trade-offs or they both behave the same way?

Thanks!

+4  A: 

Using a StringReader seems more appropriate:

using (var reader = new StringReader(someString))
{
    string line;
    while ((line = reader.ReadLine()) != null)
    {
        // You capture the line here
    }
}
Darin Dimitrov
Actually the regex shown here is just part of a bigger regex. I am not testing for a blank line.
Balaji Ramesh
Using `StringReader` allows you to capture all lines which is what I thought you were looking for. I don't see the relation with *blank line*.
Darin Dimitrov
Apologies. I should've been more clear. I wanted to capture some portions of the line using a regular expression. And within the line, after the initial regular expressions were evaluated, i wanted to capture the rest of the line using .+ Hope this makes sense. Thanks Darin for your assistance,
Balaji Ramesh
+1  A: 

I suspect that you misunderstand what the second regex, [^$]+, actually does. It matches one or more characters other than the literal $. So it will match the entire string, including line breaks. So it does something different than .+ matches. Therefore, they can't just be compared to each other.

Bart Kiers
Thanks Bart. Yes It matches the literal $. But it may not match the entire string depending on the presence of the character "$" in the input line.
Balaji Ramesh
Yes, of course: it stops at the literal `$`.
Bart Kiers
+2  A: 

What you're doing is called premature optimization. Performance is nice to have, but you should also keep your code simple and readable.
.+$ means "read everything until the end" - it is obvious, why complicate it? Later, if your program is slow, you profile it and try to figure out why (common bottlenecks are data queries and network - but don't assume anything).
This is a good example of why not to optimize too soon; having good intentions you've complicated your code and introduced errors: [^$]+$ does not have the meaning you though it had.

Kobi