I don't understand, why does the following regular expression:
^*$
Match the string "127.0.0.1"? Using Regex.IsMatch("127.0.0.1", "^*$");
Using Expresso, it does not match, which is also what I would expect. Using the expression ^.*$
does match the string, which I would also expect.
Technically, ^*$
should match the beginning of a string/line any number of times, followed by the ending of the string/line. It seems * is implicitly treated as a .*
What am I missing?
EDIT: Run the following to see an example of the problem.
using System;
using System.Text.RegularExpressions;
namespace RegexFubar
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(Regex.IsMatch("127.0.0.1", "^*$"));
Console.Read();
}
}
}
I do not wish to have ^*$ match my string, I am wondering why it does match it. I would think that the expression should result in an exception being thrown, or at least a non-match.
EDIT2: To clear up any confusion. I did not write this regex with the intention of having it match "127.0.0.1". A user of our application entered the expression and wondered why it matched the string when it should not. After looking at it, I could not come up with an explanation for why it matched - especially not since Expresso and .NET seems to handle it differently.
I guess the question is answered by it being due to the .NET implementation avoiding throwing an exception, even thought it's technically an incorrect expression. But is this really what we want?