Code Monkey home page Code Monkey logo

Comments (2)

dontangg avatar dontangg commented on August 15, 2024

I think that I have found a regular expression that will work:

var pattern = @"(?<=^([^""]*""[^""]*"")*[^""']*)(\s*'.*)$";

The fact that C# can only delimit strings with quotes makes this a little harder to read than it is. If we don't have to escape any quotes, it would look like this:

(?<=^([^"]*"[^"]*")*[^"']*)(\s*'.*)$

I'll explain it a little. You should test it too, just to make sure it works.

The end of the regular expression is the obvious part: (\s*'.*)$. It is just matching any whitespace followed by a single quote followed by anything until the end of the line. This represents the comment that we're trying to find.

The beginning of the regular expression basically says that we only find a match if there are an even number of double quotes before (or no double quotes). This makes sure that we are not inside a string when we match the single quote. Let me dive in a little more.

The expression begins with a pattern wrapped in a zero-width positive lookbehind: (?<=. This tells the engine that we only want a match if this pattern exists before everything else. It is zero-width, which means that it won't actually be part of the match. From there, it is pretty simple. It looks for anything but a ", followed by a ", followed by anything but a "", followed by a"` as many times as it can. In other words, it will match anything with two quotes as many times as it can.

Regular expressions are confusing. I hope that this helps. When I get more time, I'll test it some more and then maybe I'll add it into Language.cs. Or, you can do it and send a pull request!

from nocco.

ashclarke avatar ashclarke commented on August 15, 2024

Thanks for your time on this. I'll take a look and see if I can get it working.

from nocco.

Related Issues (9)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.