I am working on an ASP.NET project (C #) that uses regular expressions and I am having problems with end anchors! I am looking for some text, and I want to make sure that this only happens at the end of the line.
Here are some input examples that demonstrate my problem. In this case, I try to combine the word "text" at the end.
This text is my example text.
And here are the expressions I tried (with the IgnoreCase option):
text\z text\Z text$
Now here is the crazy part. None of these regular expressions work on my system (Windows 8 Pro 64-bit, VS2010, .NET 4.0). I debugged my project and I also tried the Regular Expression Tester application from the Windows 8 store. It will not match!
However, if I use an online regular expression tester, for example, Derek Slager in his blog post that works on .NET, or this Silverlight at http://regexhero.net/tester/ , using the same templates and inputs, he meets the final copy of the "text" without any problems.
I'm confused. I really need a reliable end-of-line match, and I don't know what I'm doing wrong.
EDIT: Apparently, I can't use compiled regular expressions. Here is an example of using data on which the project is actually running:
class Program { static void Main(string[] args) { string url = "http://192.168.0.113/MidlandGIS/rest/services/Osceola_Assessor_Data/MapServer/?f=pjson"; string pattern = @"mapserver/\?f=(json|pjson)$"; Regex myRegex = new Regex(pattern, RegexOptions.IgnoreCase & RegexOptions.Compiled); Console.Write("Trying compiled regex: "); if (myRegex.IsMatch(url)) Console.WriteLine("Match"); else Console.WriteLine("No match."); myRegex = new Regex(pattern, RegexOptions.IgnoreCase); Console.Write("Trying non-compiled regex: "); if (myRegex.IsMatch(url)) Console.WriteLine("Match"); else Console.WriteLine("No match."); Console.Write("Trying inline regex: "); if (Regex.IsMatch(url, pattern, RegexOptions.IgnoreCase)) Console.WriteLine("Match"); else Console.WriteLine("No match"); Console.Write("Press any key to terminate."); Console.ReadKey(); } }
exits
Compiled regular expression attempt: No match
Uncompiled regex attempt: match
Inline regex attempt: match
Press any key to complete.
Edit again: OK. I am a complete idiot. I used bitwise AND when I have to use bitwise OR to combine regex options. Compiled regex only works now.