TAGS :Viewed: 3 - Published at: a few seconds ago

I need to find all invocations of some logging macros in the code. The macro invocation is of the form:

DEBUG[1-5] ( "methodName: the logged message", arguments)

But the new versions of the macros are prepending the name of the method automatically, so my task is to write a Python script that will remove the duplicate function names specified already by the programmer.

I'm using the sub function from the re module. I plan to substitute the part indicated by || signs below :

||DEBUG[1-5] ("methodName: || the logged message", arguments) with simply DEBUG[1-5]("

The problem is following:

To find the expressions I want to substitute, I use the following regular expression:

((DEBUG | INFO | all other macros names )[1-5]*)\s*\(\"\w+:

But it doesn't match the whole expression ( from DEBUG right to the colon ), but only the macro name, that is for example DEBUG5.

Is my expression wrong or there is some quirk in the Python regex processing? ( maybe the fact that I use the DEBUG[1-5] as a subgroup has something to do with this? ) Help from anyone more knowledgable than me appreciated :).

Answer 1

try this, written in java, but you can transform it to python

replaceAll("(DEBUG[1-5]\s*\\(\")\"[^:]+:\\s+([^;]+)", "$1$2");