Regex far end knob not always working

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.

+4
source share
2 answers

Try using this:

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

See Regex for the new line (\ n) in C # for more details.

+6
source

Here's a quote right from one of the best sources of regular expressions.

Regex allows you to use the second parameter of type RegexOptions. You can specify RegexOptions.IgnoreCase as the last parameter to make case insensitive to regular register. Other options are RegexOptions.Singleline, which causes the dot to match newlines and RegexOptions.Multiline, which makes the caret and dollar match inline newlines in the subject line .

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

+2
source

Source: https://habr.com/ru/post/1444669/


All Articles