-2

I am looking for the following pattern in a java code file: (var_name = "some text") following one condition: that such var_name is not a string literal itself, being enclosed within quotations. In other words, the word "var_name" has to be followed by "=" sign followed by a string literal enclosed within quotations.

For example:

  1. valid pattern:var_name = " "; it should be a pattern match here
  2. valid pattern: another_var = " "; var_name = " " it should also be a pattern match here
  3. invalid pattern: another_var = " var_name = " it should not be a pattern match here
  4. valid pattern: another_var = " var_name = "; var_name = " " it should be a pattern match here as well.

What I have come up with : r"(?<!\".+)\s*var_name\s*=\s*[\'|\"].+[\'|\"]\s*"

However, re in python requires look-behind to have fixed-width pattern, and even this pattern doesn't find a match for the 4th example here..

So, first, If we are to modify this regex to at least find a match for the first 3 examples, how it would be like?

And second, what do you suggest for the 4th example?

I think a close solution to what I need is in the following link, but I wasn't able to modify it for this use case. Python Regex Engine - "look-behind requires fixed-width pattern" Error

1 Answers1

-1

Is it that what you were looking for? I don't know if I did understand everything

\bvar_name\b\s*=\s*("|')\s*("|')
Hans
  • 148
  • 2
  • 7