2
REGEXES = [(re.compile(r'cat'), 'cat2'),
           (re.compile(r'(if)(.*)(\r?\n)(\s*)(logger.info)(.*)'), '\1\2')]

for search, replace in REGEXES:
                    line = search.sub(replace, line)

Why is it not working on...

if( List != null ) {
   logger.info( "List is not null" );
   fieldSetContainerList.clear();
}

Works fine with Notepad++ regex search replace. Usage: Want to remove logger.info statements below all if statements.

1
  • 1
    Like @NPE said: it should work using r'\1\2')] instead of '\1\2')] Commented Jan 21, 2013 at 20:26

1 Answer 1

1

You need to use a raw string:

       (re.compile(r'(if)(.*)(\r?\n)(\s*)(logger.info)(.*)'), r'\1\2')]
                                                              ^ here

With this fix, your regex works for me. Without it, the \1 and \2 are processed when the string literal is parsed, and never make it to the regex engine.

Here is my test code:

import re

line = """if( List != null ) {
   logger.info( "List is not null" );
   fieldSetContainerList.clear();
}
"""

REGEXES = [(re.compile(r'cat'), 'cat2'),
           (re.compile(r'(if)(.*)(\r?\n)(\s*)(logger.info)(.*)'), r'\1\2')]

for search, replace in REGEXES:
    line = search.sub(replace, line)
print line

When run, this prints

if( List != null ) {
   fieldSetContainerList.clear();
}
Sign up to request clarification or add additional context in comments.

1 Comment

Not sure why its not working for me. See link for the implementation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.