Quite simple. What's the fastest regex that will return true for any input?
Edit: why the down votes? Seems like a very legitimate question. I use a function that takes a regex filter and I want to know what will make it match everything the most quickly. "Not using a regex" is not an answer开发者_JAVA技巧.
class FastestRegex
{
public static readonly Regex RE = new Regex("", RegexOptions.Compiled);
}
I would say that it would have to be something like
.*?
which would check for a match with any character, but because of the lazy operator, would match no string at all. I imagine that it would return before even checking the first character. This is assuming that the empty string doesn't return true for all inputs.
I'd guess that either the empty string, ""
, or the start-of-input, "^"
, would result in the fastest (positive) match for any string.
You should try it yourself though: running a few quick tests, the pattern "^"
is usually faster than ""
on Oracle's 1.6 JRE, but on Mono 2.4, it's the other way around. In both cases, .*?
is way slower.
But again: test things on your own system.
There is no fastest regex that never runs a regex. Therefore the fastest regex is the one that actually runs. /[\S\s]?/
or, I'm guessing this is faster /.?/s
Addon - Interresting though /^/
benched fractionally faster in perl looped 30 million times. However, pre-compiling all the regex' first slows them all down by a factor of 5. Go figure. Maybe because its too simple an expression.
精彩评论