Regex to match strings in-between double quotes that are not containing some other strings

Ilya Gazman :

How to match words between double quotes in lines not containing specific words

input:

  • System.log("error");
  • new Exception("error");
  • view.setText("message");

From the above input, I would like to ignore lines with log and Exception words in them(Case sensitive) and match words in between double quotes.

Expected output

  • message

I have been trying to use look ahead without luck

(?s)^(?!log)".+"

I need this for a search in IntelliJ using regex

The fourth bird :

In your pattern (?s)^(?!log)".+" the negative lookahead does not contain a quantifier so it will assert that what is directly after the start of the string is not log

What you could do is use a quantifier .* with an alternation to match either log or Exception and add word boundaries \b to prevent them being part of a larger word.

Then you might use negated character classes [^"] to match not a double quote and use a capturing group ([^"]+) for the value between the double quotes.

^(?!.*\b(?:log|Exception)\b)[^"]*"([^"]+)"

In Java:

String regex = "^(?!.*\\b(?:log|Exception)\\b)[^\"]*\"([^\"]+)\"";

Regex demo

If you want to make the dot to match a newline you can prepend (?s) to the pattern.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=134143&siteId=1