Home > other >  Python regex using re. to remove some combinations
Python regex using re. to remove some combinations

Time:11-25

In a file I have methods defined as follow.

@Override
PROCEDURE Prepare___ (
   attr_ IN OUT VARCHAR2 )
IS
  ----
BEGIN
   --
END Prepare___;

PROCEDURE Insert___ (
   attr_ IN OUT VARCHAR2 )
IS
  ----
BEGIN
   --
END Insert___;

Some of the are overridden and some are not.

Right now i'm using the following regex to find the procedures and functions.(this works fine)

r"^\s*((FUNCTION|PROCEDURE)\s (\w ))(.*?)BEGIN(.*?)^END\s*(\w );"

But here I want to drop the functions and procedures which has @Override annotation.

How can I modify the existing regex to do that. Or should I use a new way to do this.

this is my code

methodRegex = r"^\s*((FUNCTION|PROCEDURE)\s (\w ))(.*?)BEGIN(.*?)^END\s*(\w );"

methodMatches = re.finditer(methodRegex, fContent, re.DOTALL | re.MULTILINE | re.IGNORECASE | re.VERBOSE)
        
        for methodMatchNum, methodMatch in enumerate(methodMatches, start=1):
            methodContent=methodMatch.group()
            methodNameFull=methodMatch.group(1)
            methodType=methodMatch.group(2)
            methodName=methodMatch.group(3)

Thanks in advance

CodePudding user response:

I think what you want is a negative lookbehind. Try:

r"(?<!@Override\s)^\s*((FUNCTION|PROCEDURE)\s (\w ))(.*?)BEGIN(.*?)^END\s*(\w );"

(?<!a)b means look for b if you find it, see if there is no a before it.

  • Related