2

This regex works at online regex testers but when I try this piece of code regex does not match.

 Regex regex = new Regex(@"^\.lnr-(.*)\:before \{$", RegexOptions.Compiled | RegexOptions.Multiline);

        string css = System.IO.File.ReadAllText(Server.MapPath("/linearicons-free.css"));
        foreach (Match match in regex.Matches(css))
        {
           //doing sth 
        }

What am I missing ?

Onur Yönt
  • 31
  • 5
  • 1
    Please post a [mcve] that includes a string that this regex is tested against where you expect it to succeed but that it fails. – Lasse V. Karlsen Feb 21 '17 at 10:12
  • It appears that `$` matches at the start of `\n`, but since the file contains both `\r\n` you need to explicitly mention this in the pattern. Try this pattern: `@"^\.lnr-(.*)\:before \{\r$"` or `@"^\.lnr-(.*)\:before \{\s?$"`. This seems to be documented under [Anchors in Regular Expressions](https://msdn.microsoft.com/en-us/library/h5181w5w(v=vs.110).aspx). – Lasse V. Karlsen Feb 21 '17 at 10:18
  • String input was in online regex demo link. But also can be reached from http://demos.themecycle.com/eduhtml/edu/css/linearicons-free.css – Onur Yönt Feb 21 '17 at 10:20
  • That worked @LasseV.Karlsen thank you so much – Onur Yönt Feb 21 '17 at 10:24

1 Answers1

2

There is a \n at the end when C# is parsing the string. So your pattern^\.lnr-(.*):before \{$ fails because the last charcater you are expecting is {

Change the pattern to ^\.lnr-(.*):before \{\s$ and it should work.

you can test the .NET regexes here instead of regex101.com

Abdul Hameed
  • 1,025
  • 12
  • 27