TJ Mott
TJ Mott

Reputation: 123

Regex end-of-line anchor doesn't always work

I'm working on an ASP .NET (C#) project that makes use of regular expressions and I'm having trouble with the end-of-line anchors! I'm looking for some text and I want to make sure it only occurs at the end of a string.

Here's some sample input that demonstrates my problem. For this case I'm trying to match the word "text" at the end.

This text is my sample text

And here are the expressions I've tried (with the IgnoreCase option):

text\z
text\Z
text$

Now here's the crazy part. None of these regular expressions work on my system (Windows 8 Pro 64-bit, VS2010, .NET 4.0). I've debugged in my project and I've also tried the "Regex Tester" app from the Windows 8 store. It will not match!

However, if I use an online regex tester, for example Derek Slager's at his blog post which runs on .NET, or this Silverlight one at http://regexhero.net/tester/, using the exact same patterns and inputs, it matches the final instance of "text" with no problem.

I'm confused. I really need reliable end-of-string matching and I don't know what I'm doing wrong.

EDIT: Apparently I can't used compiled regex's. Here's an example using the data the project actually runs on:

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();
    }
}

outputs

Trying compiled regex: No match

Trying non-compiled regex: Match

Trying inline regex: Match

Press any key to terminate.

Edit again: OK I'm a complete idiot. I was using bitwise AND when I should be using bitwise OR to combine the regex options. Compiled regex's work just fine now.

Upvotes: 3

Views: 5494

Answers (2)

juFo
juFo

Reputation: 18577

Try to use this:

 (\n|\r|\r\n)

For more info see: Regex that matches a newline (\n) in C#

Upvotes: 7

Matthew Green
Matthew Green

Reputation: 10401

Here is a quote straight from one of the best regex sources out there.

The Regex allows an optional second parameter of type RegexOptions. You could specify RegexOptions.IgnoreCase as the final parameter to make the regex case insensitive. Other options are RegexOptions.Singleline which causes the dot to match newlines and RegexOptions.Multiline which causes the caret and dollar to match at embedded newlines in the subject string.

You may just need to set that option to get what you want. You can check the rest of the page for more information on .net and regex.

Upvotes: 2

Related Questions