javaregexstring-matching

How do I ignore specific character when it's inside a comments or quote using regex?


I am writing a program which can trace depth of curly braces in a code file line by line, the idea is to have a global variable incremented every-time i find {and decremented every time i find }, this is easy and I already have it done, Now I want to to make sure that I don't count the braces that are inside quotes (i.e. string) or when it's in a comment (// or /* */), I am thinking to utilize the power of regex in this problem, but I am not sure what such regex should look like, I was thinking something like "{ //^"/ "/" " but this is not correct, Any ideas?


Solution

  • You could also try to remove the comments and strings from the lines like this:

    lineString.replaceAll("\".*?\"", "")     // strings
              .replaceAll("/\\*.*?\\*/", "") // block comment
              .replaceAll("//.*", "");       // line comment
    

    Afterwards, there should be not comments and strings left. You might be able to put all this in one regex by concatenating it with |.

    Edit: I do not know whether block comments in your input span multiple lines. My proposal does not cover that case. As you process the file line by line, you would have to look for /*, set a flag, and ignore everything until you find a */.