Regex to capture VBA comments

I am trying to capture VBA comments. I have the following:

'[^";]+\Z

Which captures everything that starts with a single quote, but does not contain any double quotes until the end of the line. that is, it will not match single quotes in a double quote string.

dim s as string        ' a string variable   -- works
s = "the cat hat"    ' quote within string -- works

But it doesn’t work if the comment contains a line with two quotes

i.e.

dim s as string ' string should be set to "ten"

How can I fix my regex to handle this?

+4
source share
1 answer

@Jeff Wurz comment (^\'[^\r\n]+$|''[^\r\n]+$) , , OP, " VBA".

, , , , .

!

, VBA .

Lexers vs Parsers, @SasQ answer :

3:

, (a, b), (ab, aba, bbb etd.) (, a | b). (FSA), NFA ( ) DFA ( ). , . / (()() (()())), HTML/BBcode, ... , .

2: -

, , , . . . , / . - . . x + 3 x , ..

1: -

, , (/) , , , , - , , , .

, VBA - - ( 1), - , , , , , IsInsideQuote, ... (). , litereal: , , " " False, , .

!


:

s = "abc'def ""xyz""'nutz!" 'string with apostrophes and escaped double quotes

, 3 : "abc'def ", "xyz" "'nutz!".


# 'string with apostrophes and escaped double quotes ( ) , :

    static void Main(string[] args)
    {
        var instruction = "s = \"abc'def \"\"xyz\"\"'nutz!\" 'string with apostrophes and escaped double quotes";
        // var instruction = "s = \"the cat hat\"    ' quote within string -- works";
        // var instruction = "dim s as string ' string should be set to \"ten\"";

        int? commentStart = null;

        var isInsideQuotes = false;
        for (var i = 0; i < instruction.Length; i++)
        {
            if (instruction[i] == '"')
            {
                isInsideQuotes = !isInsideQuotes;
            }

            if (!isInsideQuotes && instruction[i] == '\'')
            {
                commentStart = i;
                break;
            }
        }

        if (commentStart.HasValue)
        {
            Console.WriteLine(instruction.Substring(commentStart.Value));
        }

        Console.ReadLine();
    }

, , Rem :

Rem this is a legal comment
' this _
    is also _
    a legal comment

, \r\n , .

+ .

+5

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


All Articles